Single layout show two posts of same category

Hi I try to display 2 other posts of the same category beside a post.

Like:

But I can’t make it work like this. What I’m doing wrong?

Hello @cvdw,

Thanks for writing to us.

Please add this query string to display the same category posts on the single post page.

 category_name={{dc:term:slug}}&post_type=post&posts_per_page=3&offset=0&ignore_sticky_posts=1&post__not_in%5B0%5D={{dc:post:id}  

Hope it helps
Thanks

Thank you!

And how not to show the current post in that list of other posts in the same category? I tried to add the condition {{dc:looper:field key=“id”}} is not {{dc:post:id}} to the looper provider but that doesn’t do the job…

THX, Carel

Hello @cvdw,

Thank you for the update.

We couldn’t open the DH Single template for some reason. Did you modify it? The query string that @prakash_s provided above should already exclude the current post from the loop, but it’s not working because there are extra characters (%5B0%5D) in the “post__not_in” parameter. We corrected the string:

category_name={{dc:term:slug}}&post_type=post&posts_per_page=3&offset=0&ignore_sticky_posts=1&post__not_in={{dc:post:id}}

Please try it again and let us know the result.

Best regards,
Ismael

Hi!

Strange, I can’t open the single layout either anymore…
I get this message:

I wil try some more…

Thank you for the update. You may need to delete the current layout and create another. Let us know if the issue persists.

I tried this now but than I get the same problem… What could that be…?

Sorry about that. Looks like the query string is causing the error. Instead of using Query String, try to set the Looper Provider to Custom, in the Hook field, use “single_layout”, then place these parameters.

{
  "current_post": "{{post.id}}",
  "category": "{{dc:term:slug}}"
}

After that, add this code in the functions.php file or use a Snippet plugin:

add_filter('cs_looper_custom_single_layout', function ($result, $args) {
    $query_args = array(
        'category'  => $args['category'],
        'post_type'      => 'post',
        'posts_per_page' => 3,
        'offset'         => 0,
        'ignore_sticky_posts' => 1,
        'post__not_in'   => array($args['current_post']),
    );

    $posts = get_posts($query_args);
    return $posts;
}, 10, 2);

These are same parameters from the previous query string. Let us know how it goes.

Thank you, I think we are almost there but now the lastest 3 post are showed but not filtered from the same category as the actual post…

We adjusted the parameter “category” to “category_name” and the value of the “current_post” parameter to “{{dc:post:id}}”. We also corrected the hook name by removing the quotation marks. Please make sure to purge the cache before testing.

YEAH! That’s it! Thank you!

Last question, but I ask this before and think it is not possible, can the next and previous buttons also can navigate in the same current category…?

Glad to know that this is now working correctly. Unfortunately, there is no option for the Post Navigation to loop within the same category.

1 Like