简体   繁体   English

在wordpress中按类别显示最受欢迎的帖子

[英]Displaying most popular post by category in wordpress

Good morning fellow coders! 各位编码员早上好!

Im having a little bit of trouble and I think its in the loop but Ive been combing the WP codex for days now and cant quite get it to work even though I know its right under my nose! 我有一点麻烦,我认为它正在循环中,但是我已经梳理了WP Codex数天了,即使我知道它在我的鼻子下面,也无法使其正常工作!

The objective is fairly simple. 目标很简单。 Im trying to build a venue & live music review site that has roughly 30 categories, 6 of them are parents and the rest are children where the parents serve as all inclusive cats for the kids. 我正在尝试建立一个场所和现场音乐评论网站,该网站大约有30个类别,其中6个是父母,其余是孩子,父母充当孩子们的全包猫。 IE: Music (parent) covers Hip Hop, R&B, etc etc. On the index I need to set it up so that the single most popular post excerpt (by view) in the parents and their associated kids show up as a headliner for each of the 6 primary categories. IE:音乐(父母)涵盖了嘻哈,R&B等。在索引上,我需要对其进行设置,以使父母及其相关孩子中单个最受欢迎的摘录(按视图显示)成为每个人的头条新闻。在6个主要类别中。 I set up a getpostviews and setpostviews function in functions.php, the setpostviews function in single.php and Ive tried setting up 'category_name = #' in the query as thats supposed to include the children, Ive tried querying by 'cat_id = #', Ive tried getting them all and then excluding the ones I dont want and I keep getting the single most popular post so Im at a loss. 我在functions.php中设置了getpostviews和setpostviews函数,在single.php中设置了setpostviews函数,我尝试在查询中设置“ category_name =#”,因为那应该包括孩子,我尝试通过“ cat_id =#”进行查询,我尝试过将它们全部收集起来,然后将我不想要的部分排除在外,而我一直在获得单个最受欢迎的帖子,因此我很茫然。

The site is currently at chronic.spearzolutions.com and you can see the specific code I have for the get/set postviews, and the loops here 该站点当前位于chronic.spearzolutions.com ,您可以在此处查看获取/设置后视图的特定代码,以及此处的循环

Im pretty sure Im just doing something wrong in the loop, but Im not too proud to admit that or ask for help. 我很确定自己在循环中做错了什么,但是我并没有因为接受或寻求帮助而感到自豪。 Always and forever "still learning" is the name of the game 永远永远的“仍然学习”是游戏的名称

Thanks in advance Nick 预先感谢尼克

Well I will start with counting the most popular posts first. 好吧,我将首先计算最受欢迎的帖子。 To Do do that, let's enqueue some view count code in the single page ( Change is_single() with your preferable condition ) 为此,让我们在单个页面中放入一些视图计数代码(用您喜欢的条件更改is_single())

For example: 例如:

define('WS_META_COUNT', 'ws93_view_count');
if(!is_admin()){

    add_action('template_redirect', 'wpse3VideoSetView');
}

function wpse3VideoSetView($postId = null){
    // substitude / add / remove your video count conditions here.

    if(!is_single())
        return;
    $id = !empty($postId) ? $postId : get_the_ID();
    $current = ( int ) get_post_meta( $id , WS_META_COUNT , true);
    $current ++;
    update_post_meta( $id , WS_META_COUNT , $current );
}

Now, when querying, I can construct a query and print, for example: 现在,在查询时,我可以构造查询并打印,例如:

$args = array('numberposts'  => -1,  /* get 4 posts, or set -1 for all */
                'orderby'      => 'meta_value',  /* this will look at the meta_key you set below */
                'meta_key'     => WS_META_COUNT,
                'order'        => 'DESC',
                'post_type'    => 'post'  /* Replace your post type here */,
                'post_status'  => 'publish');
    $query = new WP_Query( $args );

    if ( $query->have_posts() ) {
    echo '<ul>';
    while ( $query->have_posts() ) {
        $query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';

    }
    wp_reset_postdata();

It really depends on how you want to represent your data now. 这实际上取决于您现在如何表示数据。 For sub-catagory posts, query for that. 对于子类别的帖子,进行查询。 If you give me a clearer example, I can give you a better query. 如果您给我一个更清晰的例子,我可以给您一个更好的查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM