how to Sort posts by Most Recent and Most Commented filters?
how to Sort posts by Most Recent and Most Commented f开发者_高级运维ilters ?
for example http://goo.gl/K9sXz
more example : http://goo.gl/SjYjs
i found a lot on this but failed
please help me :)
I've answered a similar question here, but I'll summarise what I think will help you. It'll involve custom queries which you learn more about here.
You can use separate 'div' elements to sort each of the posts by different methods. Then you can use jQuery to hide them all (except say the 'most-recent' panel). You can then uses links to display the relevant 'div' element when a user clicks it (hiding all others) using jQuerys .click() event handler.
To populate each 'div' elements with the posts sorted by the various method you would use a custom query. For instance, to sort by the number of comments, define this function in your 'functions.php' file.
function get_most_commented($limit=10) {
global $wpdb;
$most_commented = $wpdb->get_results("SELECT comment_count, ID, post_title FROM $wpdb->posts WHERE post_type='post' AND post_status = 'publish' ORDER BY comment_count DESC LIMIT 0 , $limit");
foreach ($most_commented as $post) {
setup_postdata($post);
$id = $post->ID;
$post_title = $post->post_title;
$count = $post->comment_count;
$output .= '<li><h1><a href="'. get_permalink($id).'">'.$post_title.' - '.$count.' comments</a></h1> post_excerpt;</li>';
}
return $output;
}
then you inside the relevant 'div' element from above call the function:
<?php echo get_most_commented(15) ?>
inside an ordered/ unordered list (to display most commented 15 posts). This example just outputs the title, and the excerpt, but you could alter the HTML, and use some CSS styling to get what you want.
Hope this helps! You've inspired me to write a tutorial on this now though!
Edit: I've just looked at the Nettuts example you gave - they 're-sort' the posts by using php 'GET' method. On clicking the link you are sent to the same page but with different get variables (for example: recent=true, commented=true). The page contains a series of php if statements to determine how the posts should be sorted depending on which GET variable is set to 'true'.
This has the advantage over not requiring jQuery (or indeed javascript turned on) - and it doesn't load all three methods of sorting at once - only the one it needs.
I see what you are after now. After reading up a bit, I've realised a better (the best?) way to do it. It's a completely different approach to my first answer, so I'm posting it as a second one.
I've written a detailed tutorial here. In short you define links that point back to the page you are one, but with GET variables that tell WordPress to sort the posts in a certain way. By default WordPress sorts by date, but appending
?&orderby=comment_count&order=desc
to the url it will order it differently (by number of comments in this case). It's a bit different for post views since this is a custom field (and you'll still need the wp-post views plugin).
Hope this helps!
精彩评论