WordPress.org

Ready to get started?Download WordPress

Forums

Custom Category Templates for Child Categories - Help? (3 posts)

  1. intricateartist
    Member
    Posted 3 years ago #

    This has worked for me in the past, but it is not working now. I need some help fine tuning my code here and figuring out what I'm doing wrong.

    I have a category template. In it:

    <?php
    $category = $wp_query->get_queried_object();
    if ( $category->category_parent ) {
    	$cat_parent = $wpdb->get_var("SELECT name from $wpdb->terms WHERE term_id = $category->category_parent");
    }
     ?>
    <?php if ($category->category_parent == 4) {
     	include(TEMPLATEPATH . '/category-4.php');
    }elseif ($category->category_parent == 11) {
    	include(TEMPLATEPATH . '/category-11.php');
    	 } else {
     include(TEMPLATEPATH . '/archive.php');
     }
     ?>

    I have two category template files, as noted above. Here's category-4.php, for example:

    <ul>
    <?php query_posts( 'posts_per_page=-1' ); ?>
    <?php if( have_posts() ) : ?>
    <?php while( have_posts() ) : the_post(); ?>
    <li>
    <div class="thumb">
    <?php the_post_thumbnail(); ?>
    </div>
    </li>
    <?php endwhile; ?> <?php endif; ?>
    </ul>

    Now, category-4 displays correctly, however, the child categories of category 4 pick up the template, but they display the contents of the parent category, not just the child category. (Hope that makes sense). Every child category is showing the parent category contents.

    So, I added this to my functions:

    // ====================================
    // = Force parent category template =
    // ====================================
    function child_force_category_template($template) {
        $cat = get_query_var('cat');
        $category = get_category ($cat);
        if ( file_exists(TEMPLATEPATH . '/category-' . $category->cat_ID . '.php') ) {
            $cat_template = TEMPLATEPATH . '/category-' . $category ->cat_ID . '.php';
        }
        elseif ( file_exists(TEMPLATEPATH . '/category-' . $category->category_parent . '.php') ) {
            $cat_template = TEMPLATEPATH . '/category-' . $category->category_parent . '.php';
        }
        else{
            $cat_template = $template;
        }
      return $cat_template;
    }
    add_action('category_template', 'child_force_category_template');

    And removed this from my category template:

    <?php
    $category = $wp_query->get_queried_object();
    if ( $category->category_parent ) {
    	$cat_parent = $wpdb->get_var("SELECT name from $wpdb->terms WHERE term_id = $category->category_parent");
    }
     ?>

    But I am getting the exact same results, all child categories display the template, but the contents are all of the parent category.

    What am I missing here?

    Thank you so much for your help.

  2. intricateartist
    Member
    Posted 3 years ago #

    Okay, the problem appears to be with the query:

    <?php query_posts( 'posts_per_page=-1' ); ?>

    I've removed it, and each child category displays its posts in the parent template. So that works.

    Now I need to figure out how to set the number of posts per page.

  3. intricateartist
    Member
    Posted 3 years ago #

    For now, I've installed cbnet Different Posts Per Page plugin to manage the number of posts per category. I'm not sure why a plugin can do it and the query doesn't - if anyone knows, I'd love to know.

Topic Closed

This topic has been closed to new replies.

About this Topic