Category sidebar for single posts belonging to their category (4 posts)

  1. Sidu_88
    Posted 3 years ago #

    Hi everyone

    I'm fairly new to WordPress (and Php programming) and I’m trying to call a category sidebar for single posts that are listed under that particular category. Lets say a single post ‘Diet Coke Is Awesome’ pulls a sidebar assigned to category ‘Cold Drinks’.

    The other issue is what happens when single posts are listed under more than one category ? Lets say a single post ‘Diet Coke Is Awesome’ also happens to be listed under categories ‘Cold Drinks’ and 'Discount Supplies' where ‘Cold Drinks’ and 'Discount Supplies' are equal and separate in terms of hierarchy.

    I have sidebar files and implemented them to Categories. I would like to do the same to single posts. Any Insights will be greatly appreciated.


  2. bcworkz
    Posted 3 years ago #

    On your content-single template, you use wp_get_post_categories() to get an array of category IDs which is used in a secondary sidebar query to get posts of the same category.

    What happens with posts with more than one category is up to your code. You get an array of all the IDs, you could blindly use the first one listed, or query posts that have all the same categories, or query posts that have any one of the categories, or... anything else you can conceive.

  3. Sidu_88
    Posted 3 years ago #

    Thanks bcworkz ...greatly appreciate it for your insight

    I just found a solution borrowed from someone using wp_get_post_categories() like you suggested above which addresses the basic issue for assigning category specific sidebar for single posts. Yet there are some issues I still gotta deal with.

    if ( is_category() ) {
    $current_category_object = get_category( get_query_var( 'cat' ) );
    } else if ( is_single() ) {
    global $post;
    $post_categories = wp_get_post_categories( $post->ID );
    $current_category_object = get_category( $post_categories[0] );
    get_sidebar( $current_category_object->slug );

    I wonder if there is a way assign category sidebar to single posts based on hierarchy if single posts are assigned to multiple categories where categories are separate and not equal in hierarchy.

    Using the example from my previous post lets say single post ‘Diet Coke Is Awesome’ happens to be listed under categories ‘Cold Drinks’ and 'Discount Supplies' where ‘Cold Drinks’ is a sub-category of 'Beverages' while 'Discount Supplies' is a separate parent category.

    1. Discount Supplies
    2. Beverages
      • Cold Drinks
  4. bcworkz
    Posted 3 years ago #

    There is a way. If your coding skills are adequate, you could implement some complex algorithm that handles any complex situation exactly the way you want. You are basically going to write code that decides which index number to use instead of the '[0]' in your last snippet.

    Using your last example, for each category assigned to the single post, walk up the hierarchy, counting how many levels are required to reach the top category level (where 'parent' => 0 ). Which ever index corresponds to the fewest levels traversed would be the highest level category. In the event of a tie, you could even define a priority table of which top level category should be used before another. Or you could develop some scoring system to decide which category to use.

    The possibilities are vast, limited only by your creativity and coding skills.

Topic Closed

This topic has been closed to new replies.

About this Topic