尝试使用 PHP 在 WordPress 上创建热门帖子页面 [英] Trying to Create a Popular Posts Page on WordPress with PHP

查看:24
本文介绍了尝试使用 PHP 在 WordPress 上创建热门帖子页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常感谢您提供以下信息:不使用插件的Wordpress热门帖子,这极大地帮助我整理了自己的热门帖子我的 WordPress 网站的页面模板.但是,我认为我需要更改代码以使其表现更好,但我不确定该怎么做.

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.

新页面位于 http://sassyginger.com/most-popular-posts.它显示了两个帖子(当它应该显示五个时),然后将它们与我知道不正确的零视图"相关联.

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.

我对 PHP 非常陌生,所以如果任何人都可以为我提供有关如何调整 不使用插件的 Wordpress 热门帖子 代码显示五个帖子并去掉不正确的 0 Views 位.

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.

谢谢!

推荐答案

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

这篇关于尝试使用 PHP 在 WordPress 上创建热门帖子页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆