Well, let me first describe the full code in code comments. Please read the comments carefully-
// 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;
}
}
So for making it work on single page as well you need to modify the if
condition block and the code block will look like below-
// 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 ),
);
}
}
Notice: If it is a single post then it’ll show posts from the category(s) of current post at widget.