Trying to Create a Popular Posts Page on WordPress

2019-07-25 01:58发布

Many thanks for the information on: Wordpress popular posts without using plugins which greatly helped me put together my own Popular Posts Page Template for my WordPress site. However, I think I need to alter the code to make it behave better and am not sure what to do.

The new page is at http://sassyginger.com/most-popular-posts. It shows two posts (when it's supposed to show five), and then associates them both with "zero views" which I know isn't right.

I'm very new to PHP so would appreciate any help anyone can give me on how to tweak the Wordpress popular posts without using plugins code to show five posts and leave off the incorrect 0 Views bit.

Thank you!

1条回答
小情绪 Triste *
2楼-- · 2019-07-25 02:10

Wordpress don't track views on a post by default, so if you don't want to use a plugin, you need to create a custom field for all posts, which contains views. And then write a function that takes that value and adds one everything someone loads that page. ( Say you put the function in your functions.php ) and call it in from your single-template and sending the postid along.

function might look something like this:

function addPostView($postID) {
$views = 'post_views'; // post_views is the custom field name
$count = get_post_meta($postID, $views, true); // grab the value from that custom field

// Now we need to check that the value we just grabbed isn't blank, if it is we need to set it to 1, since it would be our first view on this post.
if($count==''){
    $count = 0;
    update_post_meta($postID, $views, '1');
}else{
    // else we can just add one to the number.
    $count++;
    update_post_meta($postID, $views, $count);
}
}

And in our single-template we would call the function somewhere like:

addPostView(get_the_ID());

Then problem number two, you can't query posts with operators, so you can't query just the five posts with highest views, so you might have to query all posts, store the views custom-field and post id in an array, then sort the array (with php's sort function). Now you got each posts ID, and that posts views in an array sorted. So take the first five (or last depending on how you sorted it) and you got the five postIDs with the highest number of views.

//ordinary wp_query
$i = 0; // keeping track of our array
//while(post-> etc....
    global $post;
    $views = get_post_meta($post->ID, 'post_views', true); // Grab our value
    /* You could also use an object here */
    $postArray[$i][0] = $views; // set it in slot $i of our array
    $postArray[$i][1] = $post->ID; // and also set the postID in the same slot

    $i++;
//endwhile;

To sort the array:

 rsort($postArray);
 $postArray = array_slice( $postArray, 0, 5 ); // grab only the first 5 values, which will be the ones with highest views.

Now you need to do a second query where you just query these IDs (with 'post__in' selector, then you can loop them out however you want.

Just note I didn't try out this code, but I've done something similar in the past. It might not be the best solution, but it will get the job done. Querying through all posts (If you have ALOT OF THEM), only to fetch five or so posts, can't be good practice :)

查看更多
登录 后发表回答