Support » Plugins » Hacks » Category/Subcategory Display Issues

  • Resolved fishfinder


    Hey Folks,

    I’ve been searching for hours to solve this one, but just can’t find a hack to get it right!

    I’d rather not use a plugin for this, but simply hard code it into my template files.

    Here’s the scenario:

    My categories have subcategories. All posts are in the sub categories with the exception of a few.

    I’d like my archives pages for categories to show subcategory links, which link to the subcategory page. I want a list of posts only to display on the subcategory page, unless the parent category has posts in it, which would be displayed on the category page.


    1st category page:
    —–SUB CAT 1
    —–SUB CAT 2
    —–SUB CAT 3
    (only show posts here if they are categorized by the parent category)

    Click on a sub category and get its page with posts:
    SUB CAT 1
    ——POST 1
    ——POST 2
    ——POST 3
    (show posts of SUB CAT 1 here)

    Hopefully that makes a little bit of sense!


Viewing 12 replies - 1 through 12 (of 12 total)
  • Still hoping to get some help with this!

    Basically looking for some code that would say:

    if category, show subcategories

    If subcategory, show posts.

    that kind of thing….




    Forum Moderator

    this will check if the category in the archive is top category:
    then show the list of child categories, and any direct posts in the category;
    if not show the posts.

    the snippet is put before the actual loop in category.php:

    <?php $this_cat = get_query_var('cat');
    if(!get_category($this_cat)->category_parent) : //check if this is top-category
    wp_list_categories('title_li=&child_of=' . $this_cat . '&show_option_none=');
    $args = array(
    'category__in' => $this_cat,
    'paged' => get_query_var('paged')
    ); //prepare to show only direct post of this category
    query_posts( $args );
    endif; // end of check ?>

    it does not check if there are posts directly in this cat, this is left to the loop; you therefore might need to make extra arangements for the ‘not found’ part of the loop.

    Hi Alchymyth,

    That is perfect, except just one thing. Can we prevent that from listing them?

    Here is the full code that I’m using, including what you have posted above.

    [code moderated as per forum rules – please use the pastebin]



    Forum Moderator

    Can we prevent that from listing them?

    what specidfic do you mean?

    listing what not?

    Oh, sorry, here’s a link to one of the pages


    you’ll see there is a bulleted list of the subcategories above the list of subcategories that I’ve already put in there.

    I’d like to either remove the bulleted list above mine, or simply style your list based on what mine currently looks like

    thanks man, this is much appreciated!


    Its in the center of the page, right below the ads.

    The site is not live, so I’m not worried about messing with code even if there are problems



    Forum Moderator

    your code sorted and with the conditional included:

    Hey Alch,

    I can see that this is moving in the right direction, but still not quite there.

    Its still listing posts on the parent category page. Except now its list all posts in order by date.

    I’m hoping to have the posts only showing up on the sub category pages, and have them include the thumbnail and the description.


    thanks for this my friend!



    Forum Moderator

    my bad – changed one bit – forgot the other:

    this here:

    <?php $args = array(
    'category__in' => $this_cat,

    should be:

    <?php $args = array(
    'category__in' => $cat,

    full template:

    Wow buddy!!!

    Thats exactly it!

    This was one of the last hurdles in getting this site ready to roll. Its replacing a horrible Joomla site that is currently being used.

    Thanks so much man!




    Forum Moderator

    you are welcome 😉

    if this is all sorted, please mark this thread as ‘resolved’ – thanks

    Yup, I was just waiting to see if you posted here again.

    Resolved, and thanks again!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Category/Subcategory Display Issues’ is closed to new replies.