[Support request] Related Posts by Tags, Categories, Latest [WP Show Posts Pro + GP Hooks]

Home Forums Support [Support request] Related Posts by Tags, Categories, Latest [WP Show Posts Pro + GP Hooks]

Home Forums Support Related Posts by Tags, Categories, Latest [WP Show Posts Pro + GP Hooks]

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #1548525
    Andrei

    Hey guys, I’m having a similar issue with no posts available to be shown as in here: https://generatepress.com/forums/topic/related-posts-by-tags-and-categories-wp-show-posts-pro-gp-hooks

    I follow this guide https://gpsites.co/merch/the-single-post/ – How I made the Related Posts. It works great just some of my categories have 1 article and I get that no posts available notice.

    What I would like to achieve in the end would be to display related posts by tag, fallback on category, and if none available to have the last fallback on most recent published articles.

    Is this possible?

    Cheers.

    #1549546
    David
    Staff
    Customer Support

    Hi there,

    try this function:

    <div class="wpsp-related-posts  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;
    $count = $cat->category_count;
    	
    if ( $count == 1 ) {
        // Set ID of fallback WPSP list 
        wpsp_display( 296 );
    } else {
        $list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' );
        wpsp_display( $list->ID, 'tax_term="' . $cat_slug . '"' );
    }
    
    ?>
    </div>

    You will need to add the WPSP List ID for the fallback list – here: wpsp_display( 296 );

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