WordPress.org

Ready to get started?Download WordPress

Forums

Child category inherit the parent category template (1 post)

  1. Opally
    Member
    Posted 2 years ago #

    Hi, the previous discussion was closed.

    The proposed plugin code is mostly good, but the children are defaulting to category.php, not the category-PARENT.php template. I want to keep category.php.

    Can you suggest a modification that would make this code work so that it doesn't default to category.php when there is an existing parent template?

    add_action('template_redirect', 'inherit_cat_template');
    
    function inherit_cat_template() {
    
      if (is_category()) {
    
        $catid = get_query_var('cat');
    
        if ( file_exists(TEMPLATEPATH . '/category-' . $catid . '.php') ) {
          include( TEMPLATEPATH . '/category-' . $catid . '.php');
          exit;
        }
    
        $cat = &get_category($catid);
        $parent = $cat->category_parent;
        while ($parent){
          $cat = &get_category($parent);
          if ( file_exists(TEMPLATEPATH . '/category-' . $cat->cat_ID . '.php') ) {
            include (TEMPLATEPATH . '/category-' . $cat->cat_ID . '.php');
            exit;
          }
          $parent = $cat->category_parent;
        }
      }
    }

Topic Closed

This topic has been closed to new replies.

About this Topic