[Resolved] Related Post

Please login to receive premium support.

Support for the free plugin can be found here.

Home Forums Pro Support Related Post

Viewing 11 posts - 31 through 41 (of 41 total)
  • Author
    Posts
  • #22223
    Tom
    Keymaster

    Let’s try this:

    <?php if ( is_single() ) {
        $cat_values = array();
    
        $cats =  get_the_category();
    
        foreach ( $cats as $cat ) {
            $cat_values[] = $cat->slug;
        }
    } else {
        $cat_values = get_category( get_query_var( 'cat' ) );
    }
    
    // Remove first category, assuming it's a parent.
    if ( count( $cat_values ) > 1 ) {
        unset( $cat_values[0] );
    }
    
    $cat_slug = implode( ', ', $cat_values );
    $list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' );
    wpsp_display( $list->ID, 'tax_term="' . $cat_slug . '"' );
    ?>
    #22225
    Samuel
    Participant

    It seems to work ! 🙂 WPSP is displaying child category on post with child category and when post has no child category it’s displaying the category.

    The only thing really weird is i got just one category and its child category that is displaying a “Sorry, no posts were found.”

    #22275
    Tom
    Keymaster

    Can you explain that a bit more? Not sure I understand.

    #22293
    Samuel
    Participant

    Sorry about that, it shows “no posts were found” because there is only one post inside this category… so it’s normal !

    To go further, I’m showing 4 related posts : Would it possible to tell WPSP to show other posts from parent category or from other category if there is less than 4 posts in the current category ?

    #22402
    Tom
    Keymaster

    You could try this:

    <?php if ( is_single() ) {
        $cat_values = array();
    
        $cats =  get_the_category();
    
        foreach ( $cats as $cat ) {
            $cat_values[] = $cat->slug;
        }
    } else {
        $cat_values = get_category( get_query_var( 'cat' ) );
    }
    
    // Remove first category as long as there's more than 4 categories.
    if ( count( $cat_values ) > 4 ) {
        unset( $cat_values[0] );
    }
    
    $cat_slug = implode( ', ', $cat_values );
    $list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' );
    wpsp_display( $list->ID, 'tax_term="' . $cat_slug . '"' );
    ?>
    #22451
    Samuel
    Participant

    Hi Tom,

    Here was the first code that was resolving the issue of “Display Child Category, and if there is not then display Parent Category” :

    // Remove first category, assuming it's a parent.
    if ( count( $cat_values ) > 1 ) {
        unset( $cat_values[0] );
    }

    If as you suggest I replace it with :

    // Remove first category as long as there's more than 4 categories.
    if ( count( $cat_values ) > 4 ) {
        unset( $cat_values[0] );
    }

    It’s displaying parent category everywhere, so first issue is coming back and it’s not displaying Child category when there is one.

    What would be awesome is :

    1 / Priority is “Get Child Category”
    2 / Then “If there is no Child Category then Get Category”
    3 / And “If there is less then 4 items in a Child Category then get Category”

    Would that be possible ?

    #22474
    Tom
    Keymaster

    What that code should do is remove the parent category if there are more than 4 categories to pull posts from.

    So if you have this:

    – Parent
    – Child
    – Child
    – Child

    Then it will get posts from the parent.

    However, if you have this:

    – Parent
    – Child
    – Child
    – Child
    – Child

    It will only pull posts from the children.

    #22497
    Samuel
    Participant

    Ok, so there is a misunderstanding.

    I’m showing 4 related posts and I was asking if it’s possible to show posts from another category or from a parent category in the case there is less then 4 posts in a child category.

    Don’t know if this make sense…

    #22518
    Tom
    Keymaster

    Ah sorry, I understand now. That gets even more complex as you need to count the posts in each category that’s being rendered in that function. Unfortunately, we’ve already gone a little out of the scope of support in this forum – something like this would take quite a lot of tweaking and playing with in order to get it working correctly.

    #22527
    Samuel
    Participant

    Ok, I understand 🙂 Thanks a lot Tom !

    #22564
    Tom
    Keymaster

    No problem!

Viewing 11 posts - 31 through 41 (of 41 total)
  • You must be logged in to reply to this topic.