是否使所有子类别使用其类别父类别的模板?

时间:2010-11-26 作者:Adam

如何使所有子类别使用其类别父级的模板?

3 个回复
最合适的回答,由SO网友:sorich87 整理而成

下面是我用来做这件事的代码:

// make category use parent category template
function load_cat_parent_template($template) {

    $cat_ID = absint( get_query_var(\'cat\') );
    $category = get_category( $cat_ID );

    $templates = array();

    if ( !is_wp_error($category) )
        $templates[] = "category-{$category->slug}.php";

    $templates[] = "category-$cat_ID.php";

    // trace back the parent hierarchy and locate a template
    if ( !is_wp_error($category) ) {
        $category = $category->parent ? get_category($category->parent) : \'\';

        if( !empty($category) ) {
            if ( !is_wp_error($category) )
                $templates[] = "category-{$category->slug}.php";

            $templates[] = "category-{$category->term_id}.php";
        }
    }

    $templates[] = "category.php";
    $template = locate_template($templates);

    return $template;
}
add_action(\'category_template\', \'load_cat_parent_template\');

SO网友:Eric B

丹尼尔·克拉布和我有同样的问题。建立this site 它有一些代码,可以在显示相应帖子的同时查找和使用父类别的模板文件。

我已经更新了它,以支持文件名中的slug(而不仅仅是类别ID)。

// Use a parent category slug if it exists
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->slug . \'.php\') ) {
        $cat_template = TEMPLATEPATH . \'/category-\' . $category ->slug . \'.php\';
    } elseif ( file_exists(TEMPLATEPATH . \'/category-\' . $category->category_parent . \'.php\') ) {
        $cat_template = TEMPLATEPATH . \'/category-\' . $category->category_parent . \'.php\';
    } else {
        // Get Parent Slug
        $cat_parent = get_category($category->category_parent);

        if ( file_exists(TEMPLATEPATH . \'/category-\' . $cat_parent->slug . \'.php\') ) {
            $cat_template = TEMPLATEPATH . \'/category-\' . $cat_parent->slug . \'.php\';
        } else {
            $cat_template = $template;
        }

    }

    return $cat_template;
}
add_action(\'category_template\', \'child_force_category_template\');

SO网友:montalvomiguelo

实际上是个过滤器挂钩

https://developer.wordpress.org/reference/functions/get_category_template/

/**
 * Retrieve path of category-videos.php template for \'videos\' subcategories
 * Filtered via \'category_template\' hook
 *
 * @param string Full path to category template file.
 *
 * @return string Full path to category template file.
 */
function get_category_videos_template( $template ) {
  $category_ID = intval( get_query_var(\'cat\') );
  $category = get_category( $category_ID );
  $parent_ID = $category->parent;
  $parent_category = get_category( $parent_ID );
  /**
   * Check wheter the $parent_category object is not a WordPress Error and its slug is \'videos\'
   */
  if ( !is_wp_error( $parent_category ) AND $parent_category->slug == \'videos\' ) {
    /**
     * Check if the template file exists
     *
     */
    if ( file_exists( TEMPLATEPATH . \'/category-\' . $parent_category->slug . \'.php\' )) {
      return TEMPLATEPATH . \'/category-\' . $parent_category->slug . \'.php\';
    }
  } else {
    return $template;
  }
}
add_filter( \'category_template\', \'get_category_videos_template\' );

结束

相关推荐

WordPress删除wp_List_Categories中最后一项的分隔符

我正在尝试删除最后一个分隔符(通常是<br/> 标记,但我将其从wp\\u list\\u categories的最后一个链接更改为“/”)。基本上我想要这个:类别1//类别2//类别3//看起来像这样:类别1//类别2//类别3以下是我当前使用的代码:<?php $cat_array = array(); $args = array( \'author\' => get_the_author_meta(\'id\'),&#x