特定类别的自定义帖子模板

时间:2012-06-16 作者:pagol

这是我的问题:我有一个父类别,下面有许多子类别,例如。

Category 
  Sub category
        sub-sub category......
        sub-sub category......
        sub-sub category......
        sub-sub category......
        sub-sub category......
        ........
我用了WP dTree 将类别显示为树的插件。现在当我点击任何sub-sub category...... 它在一个自定义帖子模板中显示该类别中的所有帖子。

我知道如何制作帖子模板,我知道当我制作任何帖子时,如果定义了模板文件,帖子将显示该模板。但是我有很多"sub-sub category......" 类别包含许多帖子,所以很难为每个帖子定义模板。

如果有任何方法可以在sub-sub category...... 类别在一个自定义模板中,然后请与我共享。。。

下面是我尝试做的一个例子:

Example of what I'm trying to do

2 个回复
最合适的回答,由SO网友:Gembel Intelek 整理而成

是否显示其父类别中的所有帖子可能是工作?

<?php
$currentCatId = get_query_var(\'cat\');
$currentCatParent = get_term_by(\'id\', $currentCatId ,\'category\');
$currentCatParentId = $currentCatParent->parent;
$my_query = new WP_Query( \'cat=\'.$currentCatParentId );
print_r($my_query);
if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post();
// Do your stuff
endwhile;
endif;
?>

SO网友:Bainternet

下面是我要做的,首先我将这个函数添加到我的主题函数中。php文件:

/**
 * Tests if any of a post\'s assigned categories are descendants of target categories
 *
 * @param int|array $cats The target categories. Integer ID or array of integer IDs
 * @param int|object $_post The post. Omit to test the current post in the Loop or main query
 * @return bool True if at least 1 of the post\'s categories is a descendant of any of the target categories
 * @see get_term_by() You can get a category by name or slug, then pass ID to this function
 * @uses get_term_children() Passes $cats
 * @uses in_category() Passes $_post (can be empty)
 * @version 2.7
 * @link http://codex.wordpress.org/Function_Reference/in_category#Testing_if_a_post_is_in_a_descendant_category
 */
if ( ! function_exists( \'post_is_in_descendant_category\' ) ) {
    function post_is_in_descendant_category( $cats, $_post = null ) {
        foreach ( (array) $cats as $cat ) {
            // get_term_children() accepts integer ID only
            $descendants = get_term_children( (int) $cat, \'category\' );
            if ( $descendants && in_category( $descendants, $_post ) )
                return true;
        }
        return false;
    }
}
然后,我创建一个函数,根据我挂钩到的父类别选择正确的模板single_template 吊钩ex:

add_action( \'single_template\', \'post_template_selector\' );
function post_template_selector($single_template) {
        global $wp_query;
        global $wp;
        if ( $wp_query->query_vars[\'post_type\'] === \'post\' ) {
                if ( have_posts()){
                        //create an array of parent category => template file name ex:
                        $cats_templates = array(
                                \'12\' => \'post_in_cat_12_file.php\',
                                \'32\' => \'post_in_cat_32_file.php\',
                                \'56\' => \'post_in_cat_56_file.php\',
                        );
                        foreach ($cats_templates as $cat => $template) {
                                if (in_category($cat) || post_is_in_descendant_category($cat))
                                        return $template;
                        }
                }
        }
        return $single_template;            
}
当心$cats_templates 数组,用于为类别Y下的帖子定义模板X。

结束

相关推荐

特定类别的自定义帖子模板 - 小码农CODE - 行之有效找到问题解决它

特定类别的自定义帖子模板

时间:2012-06-16 作者:pagol

这是我的问题:我有一个父类别,下面有许多子类别,例如。

Category 
  Sub category
        sub-sub category......
        sub-sub category......
        sub-sub category......
        sub-sub category......
        sub-sub category......
        ........
我用了WP dTree 将类别显示为树的插件。现在当我点击任何sub-sub category...... 它在一个自定义帖子模板中显示该类别中的所有帖子。

我知道如何制作帖子模板,我知道当我制作任何帖子时,如果定义了模板文件,帖子将显示该模板。但是我有很多"sub-sub category......" 类别包含许多帖子,所以很难为每个帖子定义模板。

如果有任何方法可以在sub-sub category...... 类别在一个自定义模板中,然后请与我共享。。。

下面是我尝试做的一个例子:

Example of what I'm trying to do

2 个回复
最合适的回答,由SO网友:Gembel Intelek 整理而成

是否显示其父类别中的所有帖子可能是工作?

<?php
$currentCatId = get_query_var(\'cat\');
$currentCatParent = get_term_by(\'id\', $currentCatId ,\'category\');
$currentCatParentId = $currentCatParent->parent;
$my_query = new WP_Query( \'cat=\'.$currentCatParentId );
print_r($my_query);
if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post();
// Do your stuff
endwhile;
endif;
?>

SO网友:Bainternet

下面是我要做的,首先我将这个函数添加到我的主题函数中。php文件:

/**
 * Tests if any of a post\'s assigned categories are descendants of target categories
 *
 * @param int|array $cats The target categories. Integer ID or array of integer IDs
 * @param int|object $_post The post. Omit to test the current post in the Loop or main query
 * @return bool True if at least 1 of the post\'s categories is a descendant of any of the target categories
 * @see get_term_by() You can get a category by name or slug, then pass ID to this function
 * @uses get_term_children() Passes $cats
 * @uses in_category() Passes $_post (can be empty)
 * @version 2.7
 * @link http://codex.wordpress.org/Function_Reference/in_category#Testing_if_a_post_is_in_a_descendant_category
 */
if ( ! function_exists( \'post_is_in_descendant_category\' ) ) {
    function post_is_in_descendant_category( $cats, $_post = null ) {
        foreach ( (array) $cats as $cat ) {
            // get_term_children() accepts integer ID only
            $descendants = get_term_children( (int) $cat, \'category\' );
            if ( $descendants && in_category( $descendants, $_post ) )
                return true;
        }
        return false;
    }
}
然后,我创建一个函数,根据我挂钩到的父类别选择正确的模板single_template 吊钩ex:

add_action( \'single_template\', \'post_template_selector\' );
function post_template_selector($single_template) {
        global $wp_query;
        global $wp;
        if ( $wp_query->query_vars[\'post_type\'] === \'post\' ) {
                if ( have_posts()){
                        //create an array of parent category => template file name ex:
                        $cats_templates = array(
                                \'12\' => \'post_in_cat_12_file.php\',
                                \'32\' => \'post_in_cat_32_file.php\',
                                \'56\' => \'post_in_cat_56_file.php\',
                        );
                        foreach ($cats_templates as $cat => $template) {
                                if (in_category($cat) || post_is_in_descendant_category($cat))
                                        return $template;
                        }
                }
        }
        return $single_template;            
}
当心$cats_templates 数组,用于为类别Y下的帖子定义模板X。

相关推荐