Site logo

[Resolved] How can I display a list of post after my content ends?

Home Forums Support [Resolved] How can I display a list of post after my content ends?

Home Forums Support How can I display a list of post after my content ends?

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #2227474
    Sohaib

    Actually, I am building a fairly large website that might entail 1000+ pages so I want to interlink them all.

    The latest post list is a solution but it will only display posts that are recently published.

    I want a post display list of random posts in my category and so most of my pages are interlined this way.

    Can you guys help me in this regard? Thanks

    #2227904
    Leo
    Staff
    Customer Support

    Hi there,

    You will need a third-party plugin for this.

    Maybe a related posts plugin?
    https://en-ca.wordpress.org/plugins/contextual-related-posts/

    You could also consider the query loop block in GB 1.5 which is currently in Beta:
    https://generateblocks.com/generateblocks-1-5-dynamic-data-query-loops-image-blocks/

    #2229223
    Sohaib

    Display Post

    I want to generate a list like this, I can’t seem to do it with a query loop. can you help me in this regard. thanks

    #2229282
    David
    Staff
    Customer Support

    Hi there,

    try adding this PHP Snippet:

    function db_related_posts() {
        global $post;
        // Set query args
        $args = array(
            'post_type' => 'post',
            'posts_per_page' => '3',
            'orderby' => 'rand',
            'post__not_in' => array( $post->ID ), // don't display current post         
        );
        // Optional arguments for setting category term relationship on single post
        if ( is_single() && has_category() ) {  
            $category = get_the_category($post->ID);
            $category_id = $category[0]->cat_ID;
            $category_count = $category[0]->count;
            if ( $category_count > 1 ) {
                $args['category__in'] = array($category_id);
            }
        } 
        $latest = new WP_Query($args);
        // Output loop
        if ( $latest->have_posts() ) {
            echo '<ul class="related_post_list">';     
            while ($latest->have_posts()) : $latest->the_post();
    		?>
                <li class="related-post">
                <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                </li>
    		<?php
            endwhile;
            echo '</li>';
            wp_reset_postdata();
        }
    }
    
    add_action('generate_after_content', 'db_related_posts');
    #2233290
    Sohaib

    where do I add this code ?

    #2233319
    David
    Staff
    Customer Support

    This article explains how to add PHP:

    https://docs.generatepress.com/article/adding-php/

    #2234980
    Sohaib

    okay so now the code is added and working perfectly but it’s also showing everywhere on the website. I just want to show it on posts only, not pages and homepage as well.

    can you help me in this regard? thanks.

    #2235003
    David
    Staff
    Customer Support

    Ok.
    Remove this line from the code:

    add_action('generate_after_content', 'db_related_posts');

    and add this instead:

    add_action('wp',function(){
        if ( is_single() ) {
            add_action('generate_after_content', 'db_related_posts');
        }
    });
    #2235015
    Sohaib

    It’s done!

    Well, I have to say David thanks a ton mate to you and the whole team of Generate Press.

    You guys are doing a tremendous job in helping out people with their problems. God Bless!

    #2235057
    David
    Staff
    Customer Support

    Awesome – glad to hear that, and thank you for the kind words – it is much appreciated 🙂

    #2367643
    Anjali Chawla

    Hi David,

    I want to display the related posts with featured images and post dates after every single post.

    something like this: https://prnt.sc/l2_ErmbBquGj

    I tried query loop but I couldn’t use it the way I want because I don’t have GenerateBlocks Premium. I don’t have certain parameter values like the current post, current post terms, and current post author. Can you provide me a PHP snippet for displaying the related posts in the format I want?

    Thanks

    #2367711
    David
    Staff
    Customer Support

    Hi there,

    Tom provides some code in the GB Community – see here:

    https://community.generateblocks.com/t/use-gb-query-loop-for-related-post/688/2

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.