• On a category page, how do you display a list of other categories that all posts (on that category page) have in common?

    For example, On category “A” page there are 5 posts listed. Those posts are also categorized under “B” and sub-categorized under “B-1”, “B-2”, etc. On the sidebar of category “A” page I would like to display any of the sub-categories of B (‘B-1’, ‘B-2’ and so forth).

    In other words, how to display other categories related to the posts on the ‘category A’ page, but are not necessarily sub-categories of ‘A’. Make sense?

    Any help ASAP would be great!!! Thanks.

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter stever7

    (@stever7)

    Can anyone help with this please???

    [do not bump – if it is so urgent, consider to hire someone https://jobs.wordpress.net/ – also please stop to start new topics for this problem]

    That’s a though one… my guess is you’re going to have to build it, because I don’t see loads of plugins on this subject.

    Can you code?

    My first draft would be: (somthing that gets you started)

    In your loop:

    [please read the forum guidelines for posting code – there is a limit of 10 lines of code for direct posting in this forum]

    <?php
    
        if( is_category() ){
              $categories = array();
              $cat_id = get_query_var('cat');
         }
    
         // this line is probably already in your theme; don't place it twice; it's just meant to
         // show you what goes above and below this line
         if ( have_posts() ) while ( have_posts() ) : the_post();
    
              if( is_category() ){
    
                   $cats = wp_post_get_categories( $post->ID );
                   foreach( $cats as $c ){
    
                        if( !in_array( $c, $categories ) && $c != $cat_id ){
                             $categories[] = $c;
                        }
                   }
              }
    
              //
              //     the rest of your loop
              //
    ?>

    And then in the sidebar ( make sure that -if you have a sidebar.php file, you copy that contents of that file and replace this line with it) :

    get_sidebar( );

    And add this to the top:

    <ul>
    
         <?php
         foreach( $categories as $c ){
             $cat = get_category( $c );
              echo '<li><a href="'. get_category_link( $c ).">'.$cat->name.'</a></li>'; 
    
         }?>
    
         </ul>

    By any means it’s not very ‘pretty code’ but it will probably work (haven’t tested it though).

    Thread Starter stever7

    (@stever7)

    Thanks, I’ll try that out tonight. And I’m just learning to code myself. ??

    Thread Starter stever7

    (@stever7)

    I’m getting this error on the sidebar:

    “Warning: Invalid argument supplied for foreach()”.

    Ideas?

    Ah, have you assigned categories to the posts already?
    In any case, this should fix the error (like I told you; i didn’t test ;-))

    <?php if( !empty( $categories ) ):?>
        <ul>
    
        <?php
             foreach( $categories as $c ){
    
                $cat = get_category( $c );
                echo '<li><a href="'. get_category_link( $c ).">'.$cat->name.'</a></li>'; 
    
             }
        ?>
    
         </ul>
    
    <?php endif; ?>
    Thread Starter stever7

    (@stever7)

    Hmm. I’m nor getting an error anymore, but I nothing appears.

    Again; have you actually assigned posts to the categories?

    Thread Starter stever7

    (@stever7)

    Yes, I’m getting post on the category pages. But the code above doesn’t produce any content in the sidebar.

    No worries though, I think I’m going to table this issue for a while. I’ll come back to it some other day. If you have a chance to test it out yourself LMK if you get it to work.

    Thanks Again!

    Moderator keesiemeijer

    (@keesiemeijer)

    Try it with this: https://pastebin.com/tR4tQCwk

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘display sub categories of posts on sidebar’ is closed to new replies.