wordpress latest, most comment and random log function trinity one

The latest log, the heat log, the random log these three functions in the function is very similar, nothing more than a sort of different, the latest log is sorted by the release time, hot comment log is sorted by the number of comments, random log is random Sorting. So I simply put these three functions into one, on the one hand to reduce the functionality.php code redundancy, on the other hand also facilitate their own management.
Copy the following function into functions.php in the WordPress theme directory:
function get_posts ($ orderby = ”, $ plusmsg = ”) {
$ get_posts = query_posts (‘posts_per_page = 10 & caller_get_posts = 1 & orderby =’. $ orderby);
foreach ($ get_posts as $ get_post) {
$ output = ”;
$ post_date = mysql2date ($ y month d day, $ get_post-> post_date);
$ commentcount = ‘(‘. $ get_post-> comment_count. ‘comments’);
$ post_title = htmlspecialchars (stripslashes ($ get_post-> post_title));
$ permalink = get_permalink ($ get_post-> ID);
$ output. = ‘<li> <a href=”‘. $permalink.'” title=”‘.$post_title.'”>’. $ post_title. ‘</a>’. $$ plusmsg. ‘</ li;
echo ‘<ul>’. $ output. ‘</ ul>’;
}
wp_reset_query ();
}
The calling method is as follows:
<? php
// the latest log
get_posts ($ orderby = ‘date’, $ plusmsg = ‘post_date’);
// hot comment log
get_posts ($ orderby = ‘comment_count’, $ plusmsg = ‘commentcount’);
// random log
get_posts ($ orderby = ‘rand’, $ plusmsg = ‘post_date’);
?>
You can see that they share a function of the three, but only different parameters Bale, such as the title, the latest, hot evaluation, random log function Trinity is completed.
Here I introduce the latest WordPress, hot comment, random log three-in-one function set in the time range of 30 days, the effect of the site on the sidebar of the log on it, all in 30 days within.
First put the following function in the WordPress theme folder in the functions.php:
function filter_where ($ where = ”) {
$ where. = “AND post_date>”. date (‘Y-m-d’, strtotime (‘- 30 days’)).
return $ where;
}
function some_posts ($ orderby = ”, $ plusmsg = ”, $ limit = 10) {
add_filter (‘posts_where’, ‘filter_where’);
$ some_posts = query_posts (‘posts_per_page =’. $ limit. ‘& caller_get_posts = 1 & orderby =’. $ orderby);
foreach ($ some_posts as $ some_post) {
$ output = ”;
$ post_date = mysql2date ($ y month d day), $ some_post-> post_date);
$ commentcount = ‘(‘. $ some_post-> comment_count. ‘comments’);
$ post_title = htmlspecialchars (stripslashes ($ some_post-> post_title));
$ permalink = get_permalink ($ some_post-> ID);
$ output. = ‘<li> <a href=”‘. $permalink.'” title=”‘.$post_title.'”>’. $ post_title. ‘</a>’. $$ plusmsg. ‘</ li;
echo $ output;
}
wp_reset_query ();
}
You can see the second line of 30 refers to the last 30 days, you can also modify the time according to your own needs. As for the call and upgrade before the same, the following is just more than the last one $ limit parameters:
<? php
// the latest log
some_posts ($ orderby = ‘date’, $ plusmsg = ‘post_date’, 10);
// hot comment log
some_posts ($ orderby = ‘comment_count’, $ plusmsg = ‘commentcount’, 10);
// random log
some_posts ($ orderby = ‘rand’, $ plusmsg = ‘post_date’, 10);
?>
OK, then I want to click on the highest ranking is also changed to 30 days ranking, so often to patronize the user should be more reasonable it, or it seems to look at a few, the title to see rotten.