Support » Themes and Templates » Category parent and its children to use different theme layout

  • Hi, im in the process of making a wordpress site and i want to have a catagory and its children use a different layout to the rest of the site.

    I tried category-id.php using the parents id hoping the children of that category would also use the layout but as the catagory numbers for the children are different it doesnt work. There are 27 children for the category so want to avoid using a different category-id.php for each if i can.

    I want to list the posts for this category and its children alphabetically as a list and i found this code:

    if (is_category('Glossary'))
         $posts = query_posts($query_string .
    if (have_posts()) : while (have_posts()) : the_post(); ?>

    Can i add more category id’s or names to the:

    'if (is_category('Glossary'))'

    if so what code would i need to use.

    Then presumably after the start of the loop i would show the layout for these categories as i want then would i use:

    <?php else : ?>

    and after this put how i would want every other category not listed above to show.

    Im new to wordpress and php and have jumped in the deep end making my own theme so thanks for any help in advance.


Viewing 3 replies - 1 through 3 (of 3 total)
  • I was able to add more categorys to the

    'if (is_category('Glossary'))'

    line but i realised that didnt help as it used the same theme layout just displayed it in a different order.

    After more trial and error i have managed what i wanted by placing the following code in the catagory.php:

    <?php if ( is_category(array(16,17,18)) ) {
    include(TEMPLATEPATH . '/category1.php');;
    } else {
    include(TEMPLATEPATH . '/category2.php');;

    then having category1.php styled one way for the required category and its children and having category2.php styled for all the other categories.

    Is this is a bad way of doing what i want or is there a better way ?

    Thanks in advance

    I was not sure if using the method above was ok or not so after more reading i changed the code in my category.php to:

    <?php if (is_category('category1')) { ?>
    <?php include (TEMPLATEPATH . '/category1.php'); ?>
    <?php } elseif (is_category('category2')) { ?>
    <?php include (TEMPLATEPATH . '/category1.php'); ?>
    <?php } elseif (is_category('category3')) { ?>
    <?php include (TEMPLATEPATH . '/category1.php'); ?>
    <?php } else { ?>
    <?php include (TEMPLATEPATH . '/category2.php'); ?>
    <?php } ?>

    As before if there is a better solution please let me know but for now at least it’s working as i want.

    Many thanks

    This is probably not the most elegant solution, but going off on what you said, and some code from Lorelle (, here’s what I came up with. Since I only have four categories to check, I’m not too concerned about the server load.

    Put this in the category.php page, which will now serve as a template switcher.

    <?php $this_category = get_category($cat); ?>
    <?php if ($this_category->category_parent == 0) { ?>
    <?php $this_category->category_parent = $cat; ?>
    <?php } else { ?>
    <?php $parent_category = get_category($this_category->category_parent); ?>
    if ($pid=='7')
    	include (TEMPLATEPATH . '/category-7.php');
    elseif ($pid=='6')
    include (TEMPLATEPATH . '/category-6.php');
    <?php } ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Category parent and its children to use different theme layout’ is closed to new replies.