[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 15 posts - 16 through 30 (of 41 total)
  • Author
    Posts
  • #21710
    Samuel
    Participant

    Hi,

    You proposal is returning a “Sorry, no posts were found.” message.

    BUT, I tried to replace $cat = $cats[0]; with $cat = $cats[1]; and it’s working. I didn’t know that [O] was Category (first level) and [1] was Sub-Category (second level).

    Problem now is that if posts are just in first level, it’s returning a “Sorry, no posts were found.” message.

    Would it be possible to say “Look in $cats[1] but if there is nothing in there then look in $cats[0]” ?

    Or “look in $cats[1 and O] in this order” (priority to 1)

    #21761
    Tom
    Keymaster

    Hmm, what about this?:

    if ( is_single() ) {
        $cat =  get_the_category();
    } else {
        $cat = get_category( get_query_var( 'cat' ) );
    }

    If that doesn’t work, we may be able to loop through them all and build our own array of categories.

    #21783
    Samuel
    Participant

    Hi Tom,

    This is not working, returning a “no found” message.

    #21830
    Tom
    Keymaster

    Ok, let’s try this:

    if ( is_single() ) {
        $cat = array();
    
        $cats =  get_the_category();
    
        foreach ( $cats as $cat ) {
            $cat[] = $cat->slug;
        }
    } else {
        $cat = get_category( get_query_var( 'cat' ) );
    }
    #21837
    Samuel
    Participant

    Hi,

    This is provoquing a critical error.

    We agree that the entire function would be like this ?

    <?php if ( is_single() ) {
        $cat = array();
    
        $cats =  get_the_category();
    
        foreach ( $cats as $cat ) {
            $cat[] = $cat->slug;
        }
    } 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 . '"' );
    ?>
    #21904
    Tom
    Keymaster

    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' ) );
    }
    
    $cat_slug = implode( ', ', $cat_values );
    $list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' );
    wpsp_display( $list->ID, 'tax_term="' . $cat_slug . '"' );
    ?>
    #21954
    Samuel
    Participant

    Hi Tom,

    It’s not working, it’s breaking my layout and displaying a “critical error” message where WPSP is supposed to display.

    #22004
    Tom
    Keymaster

    What’s the error, exactly?

    Using WPSP like this does take some tweaking, as it wasn’t built for it by default.

    #22008
    Samuel
    Participant

    Here it is with Wp-debug=True

    Fatal error: Uncaught Error: Cannot use object of type WP_Term as array in /home/movoco/public_html/wp-content/plugins/gp-premium/elements/class-hooks.php(196) : eval()'d code:10 Stack trace: #0 /home/movoco/public_html/wp-content/plugins/gp-premium/elements/class-hooks.php(196): eval() #1 /home/movoco/public_html/wp-includes/class-wp-hook.php(287): GeneratePress_Hook->execute_hook('') #2 /home/movoco/public_html/wp-includes/class-wp-hook.php(311): WP_Hook->apply_filters('', Array) #3 /home/movoco/public_html/wp-includes/plugin.php(478): WP_Hook->do_action(Array) #4 /home/movoco/public_html/wp-content/themes/generatepress/content-single.php(99): do_action('generate_after_...') #5 /home/movoco/public_html/wp-includes/template.php(732): require('/home/movoco/pu...') #6 /home/movoco/public_html/wp-includes/template.php(676): load_template('/home/movoco/pu...', false, Array) #7 /home/movoco/public_html/wp-includes/general-template.php(204): locate_template(Array, true, false, Array) #8 /home/movoco/public_html/wp-content/them in /home/movoco/public_html/wp-content/plugins/gp-premium/elements/class-hooks.php(196) : eval()'d code on line 10

    #22113
    Tom
    Keymaster
    #22131
    Samuel
    Participant

    Hi Tom,

    Unfortunately this snipet is displaying posts from category but not from sub-category.

    #22171
    Tom
    Keymaster

    Can you share the debug data with this change?

    #22175
    Samuel
    Participant

    Hi Tom,

    Here you go :

    array(7) { ["order"]=> string(3) "ASC" ["orderby"]=> string(6) "parent" ["post_type"]=> string(4) "post" ["posts_per_page"]=> int(4) ["post_status"]=> array(1) { [0]=> string(7) "publish" } ["tax_query"]=> array(1) { [0]=> array(4) { ["taxonomy"]=> string(8) "category" ["field"]=> string(4) "slug" ["terms"]=> array(2) { [0]=> string(6) ""andes" [1]=> string(10) "santander"" } ["operator"]=> string(2) "IN" } } ["post__not_in"]=> array(1) { [0]=> int(22765) } }

    #22213
    Tom
    Keymaster

    So that means the list is now looking for posts in the “andes” and “santander” categories.

    Things get pretty complex once you start checking for parent/child categories, which is why related post plugins are much better suited for this kind of thing.

    Is “santander” a child category within the “andes” category?

    #22221
    Samuel
    Participant

    Hi Tom,

    Yes, “santander” a child category within the “andes” category

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