侧边栏按当前类别显示帖子也显示在单个帖子中

时间:2017-09-25 作者:didi

我在侧边栏中找到了这段代码,它显示了该类别的所有相关帖子,但当我单击一篇帖子时,它就不起作用了(显示了最近的文章)。

我希望在单个帖子中也有同样的行为。

我必须在代码中添加什么?

add_filter( \'tve_dash_enqueue_frontend\', \'__return_true\' );

add_filter( \'widget_posts_args\', \'my_widget_posts_args\');
function my_widget_posts_args($args) {
if ( is_category() ) {
    $cat = get_queried_object();
    return array(
        \'posts_per_page\' => 10,
        \'no_found_rows\' => true, 
        \'post_status\' => \'publish\', 
        \'ignore_sticky_posts\' => true,
        \'cat\' => $cat -> term_id//the current category id
         );
}
else {//keeps the normal behaviour if we are not in category context
    return $args;

}}
谢谢你帮助我。

1 个回复
SO网友:CodeMascot

首先,让我在代码注释中描述完整的代码。请仔细阅读评论-

// By this below code you are returning TRUE to the filter \'tve_dash_enqueue_frontend\' filter. Which is I think, is turning something on.
add_filter( \'tve_dash_enqueue_frontend\', \'__return_true\' );

// Here you are hooking the `widget_posts_args` filter with `my_widget_posts_args` function.
add_filter( \'widget_posts_args\', \'my_widget_posts_args\');
function my_widget_posts_args($args) {
    // Look here, this is the twist.
    // with checking on `is_category()` function you are making this behavior for categories only.
    if ( is_category() ) {
        $cat = get_queried_object();
        return array(
            \'posts_per_page\' => 10,
            \'no_found_rows\' => true,
            \'post_status\' => \'publish\',
            \'ignore_sticky_posts\' => true,
            \'cat\' => $cat -> term_id//the current category id
        );
    }
    else {
        // If it is not category it is returning `$args` for normal behaviour.
        return $args;

    }
}
因此,要使其在单个页面上工作,您需要修改if 条件块和代码块如下所示-

// By this below code you are returning TRUE to the filter \'tve_dash_enqueue_frontend\' filter. Which is I think, is turning something on.
add_filter( \'tve_dash_enqueue_frontend\', \'__return_true\' );

// Here you are hooking the `widget_posts_args` filter with `my_widget_posts_args` function.
add_filter( \'widget_posts_args\', \'my_widget_posts_args\' );
function my_widget_posts_args( $args ) {
    // Look here, this is the twist.
    // with checking on `is_category()` function you are aking this behaviour for categories only.
    if ( is_category() ) {
        $cat = get_queried_object();
        return array(
            \'posts_per_page\' => 10,
            \'no_found_rows\' => true,
            \'post_status\' => \'publish\',
            \'ignore_sticky_posts\' => true,
            \'cat\' => $cat -> term_id, //the current category id
        );
    } else {
        // If it is a single post then it\'ll return posts from the category(s) of current post.
        global $post;
        return array(
            \'posts_per_page\' => 10,
            \'no_found_rows\' => true,
            \'post_status\' => \'publish\',
            \'ignore_sticky_posts\' => true,
            \'category__in\' => get_the_category( $post->ID ),
        );

    }
}
注意:如果它是一篇文章,那么它将在widget上显示当前文章类别中的文章。

结束

相关推荐

Ignore latest two posts

我试图从一个页面中排除最近的两篇博客文章。我知道这是可能的offset 然而,这样做会导致一个bug,其中一些博客帖子会在第二页上重复,因此并不理想。目前,我正在使用post id手动执行此操作,如下所示:$paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : \'1\'; $args = array( \'posts_per_page\' => 5, \'post__not