首先,让我在代码注释中描述完整的代码。请仔细阅读评论-
// 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上显示当前文章类别中的文章。