• Resolved Resy

    (@selectedbymila)


    Hi there,

    For my website I use a FSE/blcok theme, where I created all my templates with the standard Gutenberg blocks. Now I am struggling with something related to my template for single blog posts.

    At the end of each post I use the Query Loop to automatically show the 3 most recent posts.

    The problem is, I use this template for all my blog posts and ideally I want the query loop to only show the 3 most recent posts from the same category as the post you are viewing. So for instance, if you read a post that’s part of the Producitivity category, then you would only see the 3 latest posts from that same category.

    I know that when you add the query loop block, you can select a category in the block settings. However, that then applies to all the posts – also to the ones within different categories.

    So the question is; is there a way that the query loop automatically only shows posts from the same category as the current post?

    I am not very good with coding, so I really hope someone can help me with this.

    Thanks in advance!!

    The page I need help with: [log in to see the link]

Viewing 9 replies - 1 through 9 (of 9 total)
  • Hello @selectedbymila
    For shows posts from the same category as the current post code is:

    function wpb_postsbycategory() {
    // the query
    $term_list = get_the_terms(get_the_id(), 'category');
    $the_query = new WP_Query( array( 
        'category_name' => $term_list[0]->name, 
        'posts_per_page' => 3 
    ) ); 
        
    // The Loop
    if ( $the_query->have_posts() ) {
        $string .= '<ul class="postsbycategory widget_recent_entries">';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
                if ( has_post_thumbnail() ) {
                $string .= '<li>';
                $string .= '<a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_post_thumbnail($post_id, array( 50, 50) ) . get_the_title() .'</a></li>';
                } else { 
                // if no featured image is found
                $string .= '<li><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() .'</a></li>';
                }
                }
        } else {
        // no posts found
     $string .= '<li>No Posts Found</li>';
    }
    $string .= '</ul>';
        
    return $string;
        
    /* Restore original Post Data */
    wp_reset_postdata();
    }
    // Add a shortcode
    add_shortcode('categoryposts', 'wpb_postsbycategory');
    Thread Starter Resy

    (@selectedbymila)

    @rohitmukati thank you for your quick reply. I really appreciate that!

    Sorry, here comes the beginner question: how and where exactly should I place this?

    I already tried to copy it and post it in the code via Editor > Template > Single post > Options > Code editor, but that didn’t work…

    Add this code in function.php and use shortcode in you template as normal [categoryposts] and if you want to use this in php file then use like this echo do_shortcode(‘[categoryposts ]’);

    Moderator bcworkz

    (@bcworkz)

    Add this code in functions.php

    That will work… for a while, until your theme updates. Then you’d need to re-apply the code again. To avoid this, create a child theme and place your code in the child functions.php. If your theme has no functions.php file, you could in theory add a new file, but it’s much better to create a child theme with one anyway.

    Creating a child theme if you are not that comfortable with PHP code can be a little tricky. But only a little. Shortcode creation like rohitmukati has suggested can instead reside in a custom plugin. These are a little easier to create than child themes. But if you anticipate creating custom templates and other theme variants besides adding to functions.php, a child theme is preferable.

    Thread Starter Resy

    (@selectedbymila)

    I indeed don’t want to add something to the function.php file since I know about the reset when updating the theme.
    Instead I use the WPcode plug in and saw that you can also use that for a php snippet. However, when I copy paste the full code @rohitmukati shared in there, and add the shortcode in my block theme template, it doesn’t show anything… Do I need to change something in his code since I do add the code via the WPcode plug in?

    Sorry, I just try to understand it and get more familiar with this part of blogging…

    @selectedbymila how are you adding the code and shortcode, please share screen short and also share screenshot after adding code front side what you see

    Thread Starter Resy

    (@selectedbymila)

    @rohitmukati I recorded a loom to show what I am doing to add it to my website. Thought this might be easier so you can see which steps I take. You can see it here: https://www.loom.com/share/a182d528838346ed98dd6f7eab29bdca?sid=c53faf70-4a2c-4ec1-90a3-3eb9c31089e8

    If you still want some screenshots, please let me know.

    Side note: besides adding “categoryposts” as shortcode, I also tried it with [categoryposts], wpb_postsbycategory, and [wpb_postsbycategory] – but nothing works…

    @selectedbymila
    This is good you make video
    You use shortcode without brackets you must use with brackets like this [categoryposts] and you need use other code in Appearance > Theme File Editor > function.php not in code snippets.

    Thread Starter Resy

    (@selectedbymila)

    @rohitmukati I also tried it with the brackets but unfortunately that didn’t help. It’s unfortunate that it’s not possible via a php code snippet, and needs to be in the function.php file.

    Since I don’t want to do that, to avoid breaking something, I will look for a work around. Maybe create templates per category or via a plug in.

    So although it won’t work for me, I really appreciate the time and effort you put into this to try to help me! Have a good day 🙂

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Showing current category posts in “recent posts”-block’ is closed to new replies.