0

I've got this code that works fine in the sidebar and it shows all the related post for the category, but when I click on a single post not works (shows recent articles).

I would like the same behaviour also in single post.

What I have to add in the code?

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;

}}

Thanks for help me.

2

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.

  • @didi Please remove your comment what you did as answer and try the updated code. And for further communication must use comment, NOT ANSWER – CodeMascot Sep 25 '17 at 10:20
  • Sorry for use answer! By the way, now the error it's gone, but nothing appear in the sidebar. – didi Sep 25 '17 at 11:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.