WordPress.org

Forums

display sub categories of posts on sidebar (11 posts)

  1. stever7
    Member
    Posted 3 years ago #

    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.

  2. stever7
    Member
    Posted 3 years ago #

    Can anyone help with this please???

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

  3. LucP
    Member
    Posted 3 years ago #

    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?

  4. LucP
    Member
    Posted 3 years ago #

    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).

  5. stever7
    Member
    Posted 3 years ago #

    Thanks, I'll try that out tonight. And I'm just learning to code myself. :)

  6. stever7
    Member
    Posted 3 years ago #

    I'm getting this error on the sidebar:

    "Warning: Invalid argument supplied for foreach()".

    Ideas?

  7. LucP
    Member
    Posted 3 years ago #

    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; ?>
  8. stever7
    Member
    Posted 3 years ago #

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

  9. LucP
    Member
    Posted 3 years ago #

    Again; have you actually assigned posts to the categories?

  10. stever7
    Member
    Posted 3 years ago #

    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!

  11. keesiemeijer
    moderator
    Posted 3 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic