Display sub-categories of category? (8 posts)

  1. sincewelastspoke
    Posted 7 years ago #

    I have my page 'archive.php' display all my posts from where catID=11.

    Is there a way I can list either a) the posts from my sub-categories grouped or b) the sub-categories themselves ?

    So if catID=11 was 'Sport' and some subs of that were 'Football'(ID=12) and 'Golf'(ID=13), it would output....

    - Football
    - Golf

    Any ideas? Thanks for the help.


    Found a solution and edited it a bit:

    <?php if (is_category(11) or ($parent == '11')) : // Quote
    							$categories = get_categories('child_of=11');
    							foreach ($categories as $cat) {
    								$option = '<a href="/category/archives/'.$cat->category_nicename.'">';
    								$option .= $cat->cat_name;
    								$option .= ' ('.$cat->category_count.')';
    								$option .= '</a>';
    								echo $option;
    				  ?><?php endif; ?>

    Cheers anyways folks!

  2. designsimply
    Posted 7 years ago #

    Try this.

    <?php wp_list_categories('title_li=&include=12'); ?>
    <ul><?php wp_list_categories('title_li=&child_of=12&show_count=1'); ?></ul>

    See wp_list_categories in the documentation for more details and to see all the avail parameters:

  3. sincewelastspoke
    Posted 7 years ago #

    Me again!

    I have a cat=11 for posts and sub-categories within it(e.g. 14) which have posts.

    Is there a function for the parent category?
    So if a sub category has a parent category of 11, do this....

    Thank you!

  4. I think most of your questions can be answered by checking out this page...


    The wp_list_categories is a very robust function capable of excluding specific categories or starting with a specific category and displaying all of the children of it...

    Check it out...

  5. sincewelastspoke
    Posted 7 years ago #

    Thanks for the reply.

    OK, used this:
    <?php echo(get_category_parents($cat, TRUE, ' &raquo; ')); ?>

    which gives me Main Category >> Subcategory

    However, I need to use the Main Category ID and I can't seem to retrieve it from that function?

    Thanks for any advice.

  6. sincewelastspoke
    Posted 7 years ago #

    That bit of code is displaying the Main Category ID, but I need to find a way to store the ID value in a variable.


  7. http://codex.wordpress.org/Template_Tags/get_the_category

    I think I may have misunderstood your question, but I also think this might be simpler than my brain is allowing it to be right now...

    Let me revisit this tomorrow and report back with what I come up with... :)

  8. designsimply
    Posted 7 years ago #

    @sincewelastspoke, if you are on the page itself (i.e. page.php), you could use this:
    if ($post->post_parent=="11") { ... }

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.