[Support request] Related Posts

Home Forums Support [Support request] Related Posts

Home Forums Support Related Posts

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #1560210
    Kumi

    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?

    #1560568
    David
    Staff
    Customer Support

    Hi there,

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

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