且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

尝试使用 PHP 在 WordPress 上创建热门帖子页面

更新时间:2023-12-01 23:11:52

Wordpress 默认不跟踪帖子的浏览量,所以如果你不想使用插件,你需要为所有人创建一个自定义字段帖子,其中包含视图.然后编写一个函数,该函数接受该值并将某人加载该页面的所有内容添加一个.(假设您将函数放在您的 functions.php 中)并从您的单一模板中调用它并发送 postid.

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());

然后问题二,你不能用操作符查询帖子,所以你不能只查询浏览量最高的五个帖子,所以你可能需要查询所有帖子,将视图自定义字段和帖子ID存储在一个数组,然后对数组进行排序(使用 php 的排序功能).现在您获得了每个帖子 ID,并且在一个数组中排序了帖子视图.因此,取前五个(或最后一个,取决于您的排序方式),您将获得观看次数最多的五个 postID.

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;

对数组进行排序:

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

现在您需要进行第二次查询,您只需查询这些 ID(使用post__in"选择器,然后您可以随意循环它们.

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 :)