在wordpress中按类别显示最热门的帖子

时间:2015-02-17 19:03:55

标签: php wordpress function loops categories

早上好的编码员!

我有一点点麻烦,我认为它在循环中,但我已经梳理了WP codex好几天了,即使我知道它在我的鼻子底下也不能让它工作!

目标很简单。我试图建立一个场地&现场音乐评论网站有大约30个类别,其中6个是父母,其余是孩子,父母为孩子们提供全包猫。 IE:音乐(父母)涵盖嘻哈,R& B等等。在索引上我需要设置它,以便父母及其相关孩子中最受欢迎的一个帖子摘录(通过视图)显示为头条新闻对于6个主要类别中的每一个。我在functions.php中设置了一个getpostviews和setpostviews函数,在single.php中设置了setpostviews函数,我尝试在查询中设置'category_name =#',因为它应该包含子项,Ive尝试通过'cat_id =#'查询,我试过把它们全部拿出然后排除我不想要的那些,我继续得到最受欢迎的帖子,所以我不知所措。

该网站目前位于chronic.spearzolutions.com,您可以看到我为获取/设置的帖子设置的具体代码,以及循环here

我很确定我只是在循环中做错了什么,但我不太自豪地承认或寻求帮助。永远和永远“仍在学习”是游戏的名称

提前致谢 尼克

1 个答案:

答案 0 :(得分:1)

好吧,我将首先计算最受欢迎的帖子。要做到这一点,让我们在单个页面中排队一些视图计数代码(使用您的首选条件更改is_single())

例如:

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

现在,在查询时,我可以构建查询并打印,例如:

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

这实际上取决于您现在想要如何表示数据。对于子类别帖子,请查询。如果你给我一个更清晰的例子,我可以给你一个更好的询问。

相关问题