• Resolved mian117

    (@mian117)


    Hi @héctor Cabrera,
    I used the shortcode to display related posts by current posts category. It shows the most viewed 10 posts from that category in the sidebar of posts. Like on this post, But on some posts it throws 1 or 2 posts from a different category Like in the given link in the sidebar 6th post is not from the category of the current post. It might be a glitch or maybe I did something wrong, any help would be appreciated. Thank You

    Shotcode I used
    echo do_shortcode("[wpp cat='$top_level_cat' limit=10 stats_views=0 range='last24hours' thumbnail_width=157 order_by='views']");
    PS. $top_level_cat variable pass the current category ID.

    • This topic was modified 2 years, 6 months ago by mian117.
    • This topic was modified 2 years, 6 months ago by mian117.

    The page I need help with: [log in to see the link]

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hi @mian117,

    I’d need to see some more of your code so I can help. How are you getting the current category ID exactly?

    Thread Starter mian117

    (@mian117)

    Hi @hcabrera,
    Here is the code which gets the top-level category.

    function smart_category_top_parent_id ($catid) {
    	while ($catid) {
    		$cat = get_category($catid);
    		$catid = $cat->category_parent; 
    		$catParent = $cat->cat_ID;
    	}
    	return $catParent;
    }
    
    $postID =  get_queried_object_id();
    
    $category = get_the_category( $postID );
    $catid = $category[0]->cat_ID;
    
    $top_level_cat = smart_category_top_parent_id ($catid);
    Plugin Author Hector Cabrera

    (@hcabrera)

    Alright, I’ll give that a test once I get some free time and report back.

    One thing I’ll share right away -if you don’t mind some feedback- is that your smart_category_top_parent_id() function could use some minor code improvements.

    Plugin Author Hector Cabrera

    (@hcabrera)

    Oh, one more thing. How do you usually categorize your posts? Do you assign both the parent category and the child category to a post? Just the child category?

    Thread Starter mian117

    (@mian117)

    I assign both, parent and child.

    Following this…
    Any updates @hcabrera

    Thanks!

    Plugin Author Hector Cabrera

    (@hcabrera)

    Nope, no updates unfortunately. Have been busy with work and so haven’t had the chance to get a test site up & running to test this yet @nextbracket.

    Plugin Author Hector Cabrera

    (@hcabrera)

    Hey @mian117 & @nextbracket,

    Sorry this took so long, it’s been a couple of busy weeks for me.

    So I gave this some thought and came with a slightly different solution. Without further ado, it looks something like this:

    <?php
    function get_top_category($catid) {
    	$cat = null;
    
    	do {
    		$cat = get_category($catid);
    		$catid = $cat->cat_ID;
    	} while( $cat->category_parent );
    	
    	return $cat;
    }
    
    if ( is_single() ) :
    
    	$postID =  get_queried_object_id();
    
    	$category = get_the_category( $postID );
    	$catid = $category[0]->cat_ID;
    	$top_category = get_top_category($catid);
    
    	?>
    	<h2><?php printf( __('Most Popular In %s'), $top_category->name); ?></h2>
    
    	<?php
    	echo do_shortcode("[wpp cat='{$top_category->cat_ID}' limit=10 stats_views=0 stats_category=1 range='last24hours' thumbnail_width=157 order_by='views']");
    
    endif;
    ?>

    If you have any comments / questions please let me know.

    Plugin Author Hector Cabrera

    (@hcabrera)

    Also, please keep in mind that:

    1. The code above will only work when viewing a post.
    2. The code assumes that all posts are assigned both the parent category(ies) and a child category (or multiple child categories).
Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Random Post in Current Posts categories Related Posts’ is closed to new replies.