WordPress.org

Ready to get started?Download WordPress

Forums

sub_category description (3 posts)

  1. jfarts
    Member
    Posted 3 years ago #

    After trying this on my own for quite some time now, I'm posting my problem here. Hopefully there is somebody who knows how to solve this.

    On my site I have got 4 parent categories which all have a couple of child categories. Each child category has a couple of posts.
    For each child category I would like a description of that category to appear above the posts as sort of a "sticky" description.
    I have been trying this with a piece of code I found on this forum and some of my own additions:

    <?php
    if (is_tag());
    else
    $category = get_the_category('');
    $cat_slug = $category[0]->category_nicename;
    if (file_exists(TEMPLATEPATH. '/cat_description/'.$cat_slug.'.php') ) {
    include (TEMPLATEPATH . '/cat_description/'.$cat_slug.'.php');
    echo " ";
    } else {
    echo '';
    }
    ?>

    What is does is check the folder ".../cat_description" to see if there is a file named category-slug.php which matches the category slug of the current page. If it does, then the content of the category-slug.php file is shown, which is just plain HTML.
    I have placed this piece of code in my archive.php and at first it seemed to work. But then I found out it only works for some sub-categories, while for others, there is no description shown.

    As it turns out, when you get the categories name with get_the_category, you do not get the name of the current, but of the parent category. For example, I got a parent category called "Augmented Reality" and two child categories named "Augmented Anatomy" and "Augmented Subsurface".
    For one of the two I get the right category name when I get it through get_the_category, but for the other I get the parents' category name and no description is shown.

    Does anybody know a solution to the get_the_category problem for child categories?

  2. alchymyth
    Forum Moderator
    Posted 3 years ago #

    in a category archive this should work:

    $category = get_category(get_query_var('cat'));
    $cat_slug = $category->slug;
  3. jfarts
    Member
    Posted 3 years ago #

    alchymyth, you're the man! Totally made my day.

Topic Closed

This topic has been closed to new replies.

About this Topic