WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. Sidu_88
    Member
    Posted 11 months 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.

    http://www.espenvogel.com
    Thanks

  2. bcworkz
    Member
    Posted 11 months 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
    Member
    Posted 11 months 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
    Member
    Posted 11 months 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.

Reply

You must log in to post.

About this Topic