WordPress.org

Support

Support » Plugins and Hacks » Hacks » [Resolved] Category/Subcategory Display Issues

[Resolved] Category/Subcategory Display Issues

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

    Example:

    1st category page:
    CATEGORY 1
    —–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
    etc.
    (show posts of SUB CAT 1 here)

    Hopefully that makes a little bit of sense!

    Dave

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

    thanks,
    Dave

    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]

    http://pastebin.com/i4DNsmVy

    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


    Site

    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!

    dave

    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

    your code sorted and with the conditional included:

    http://pastebin.com/yQK28u5X

    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.

    site

    thanks for this my friend!

    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: http://pastebin.com/TWxE0dHE

    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!

    Dave

    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 ‘[Resolved] Category/Subcategory Display Issues’ is closed to new replies.
Skip to toolbar