Site logo

Archived topic

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

11 replies · Started by Sohaib on May 21, 2022

Viewing posts 1–12 of 12

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

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

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');

where do I add this code ?

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.

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');
    }
});

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!

Awesome - glad to hear that, and thank you for the kind words - it is much appreciated :)

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

This archived topic is closed to new replies.