WordPress.org

Ready to get started?Download WordPress

Forums

Category parent and its children to use different theme layout (4 posts)

  1. noonoo
    Member
    Posted 5 years ago #

    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:

    <?php
    if (is_category('Glossary'))
    {
         $posts = query_posts($query_string .
    '&orderby=title&order=asc&posts_per_page=-100');
    }
    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.

    Nick

  2. noonoo
    Member
    Posted 5 years ago #

    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

  3. noonoo
    Member
    Posted 5 years ago #

    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

  4. GeekAcademic
    Member
    Posted 5 years ago #

    This is probably not the most elegant solution, but going off on what you said, and some code from Lorelle (http://codex.wordpress.org/User:Lorelle/Custom_Category_Template), 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); ?>
    
    <?php
    $pid=$parent_category->cat_ID;
    if ($pid=='7')
    	include (TEMPLATEPATH . '/category-7.php');
    elseif ($pid=='6')
    include (TEMPLATEPATH . '/category-6.php');
    
    else
    
    ?>
    
    <?php } ?>

Topic Closed

This topic has been closed to new replies.

About this Topic