Site logo

Archived topic

Related Posts

1 reply · Started by Kumi on December 1, 2020

Viewing posts 1–2 of 2

Hello GP,

I'm trying to add related post after a blog post without using a plugin.
I follow what I see on this site: https://www.wplogout.com/display-related-posts-in-generatepress-theme/

I make a hook and add this code

<div class="wpsp-related-posts1  grid-container">
	<h2>Related Posts</h2>
<?php
if ( is_single() ) {
    $cats =  get_the_category();
    $cat = $cats[0];
} else {
    $cat = get_category( get_query_var( 'cat' ) );
}

$cat_slug = $cat->slug;
$list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' );
wpsp_display( $list->ID, 'tax_term="' . $cat_slug . '"' );
?>
</div>

It says nothing can be found.

I proceed to add this code to show related post determined by tags and get a weird error on line 7.

<div class="wpsp-related-posts1  grid-container">
	<h2>Related Posts</h2>
<?php    
if ( is_single() ) {
        $tags =  get_the_tags();
        $tags_list = [];
        foreach ($tags as $tag)
            $tags_list[] = $tag->slug;
        $tag_string = implode( ', ', $tags_list);
    } else {
        $tag_string = get_tag( get_query_var( 'tag' ) );
    }

    $list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' );
    wpsp_display( $list->ID, 'tax_term="' . $tag_string . '"' );
    ?>
</div>

Can anyone tell me what I'm doing wrong?

Is there any alternative to add related posts?

Hi there,

if it displays No Posts Found then make sure you have more Posts in the same category.

This archived topic is closed to new replies.