允许基于类别的单一帖子模板的功能冲突

时间:2011-04-03 作者:Christopher

我用过Justin Tadlock\'s great function for category specific custom post templates 很多时候,您可以使用single-cat-id.php 对于属于特定类别的单个职位。我在一个新网站上工作时遇到了一个问题,需要使用single-{posttype} 自定义帖子类型的模板。该函数似乎阻止自定义帖子使用single-{posttype} 样板

Here\'s the applicable code:

/* Define a constant path to our single template folder */
define(SINGLE_PATH, TEMPLATEPATH . \'/single\');

/* Filter the single_template with our custom function*/
add_filter(\'single_template\', \'my_single_template\');

/* Single template function which will choose our template*/
function my_single_template($single) {
    global $wp_query, $post;

/* Checks for single template by category. Check by category slug and ID */
foreach((array)get_the_category() as $cat) :

    if(file_exists(SINGLE_PATH . \'/single-cat-\' . $cat->slug . \'.php\'))
        return SINGLE_PATH . \'/single-cat-\' . $cat->slug . \'.php\';

    elseif(file_exists(SINGLE_PATH . \'/single-cat-\' . $cat->term_id . \'.php\'))
        return SINGLE_PATH . \'/single-cat-\' . $cat->term_id . \'.php\';

endforeach;

/*Checks for default single post files within the single folder */
if(file_exists(SINGLE_PATH . \'/single.php\'))
    return SINGLE_PATH . \'/single.php\';

elseif(file_exists(SINGLE_PATH . \'/default.php\'))
    return SINGLE_PATH . \'/default.php\';

return $single; }
关于如何修改以下代码以允许single-cat-id 以及single-{posttype}?

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

您可以检查您的post-type-single-{post-type}。php首先尝试以下操作:

/* Define a constant path to our single template folder */
define(SINGLE_PATH, TEMPLATEPATH . \'/single\');

/* Filter the single_template with our custom function*/
add_filter(\'single_template\', \'my_single_template\');

/* Single template function which will choose our template*/
function my_single_template($single) {
    global $wp_query, $post;

/* Checks for single template by post type */
if ($post->post_type == "POST TYPE NAME"){
    if(file_exists(SINGLE_PATH . \'/single-\' . $post->post_type . \'.php\'))
        return SINGLE_PATH . \'/single-\' . $post->post_type . \'.php\';
}

/* Checks for single template by category. Check by category slug and ID */
foreach((array)get_the_category() as $cat) :

    if(file_exists(SINGLE_PATH . \'/single-cat-\' . $cat->slug . \'.php\'))
        return SINGLE_PATH . \'/single-cat-\' . $cat->slug . \'.php\';

    elseif(file_exists(SINGLE_PATH . \'/single-cat-\' . $cat->term_id . \'.php\'))
        return SINGLE_PATH . \'/single-cat-\' . $cat->term_id . \'.php\';

endforeach;

/*Checks for default single post files within the single folder */
if(file_exists(SINGLE_PATH . \'/single.php\'))
    return SINGLE_PATH . \'/single.php\';

elseif(file_exists(SINGLE_PATH . \'/default.php\'))
    return SINGLE_PATH . \'/default.php\';

return $single; 
}
只需将“POST TYPE NAME”替换为自定义的POST TYPE NAME即可。

结束

相关推荐

WordPress自定义帖子类型Single.php?

我有一个WordPress自定义帖子类型设置。我已经创建了single-[customposttype].php 但是,它不会只显示请求的自定义帖子类型,而是转到URL,然后显示自定义类型中的所有帖子。下面是我当前使用的代码的副本:<?php query_posts(\"post_type=shorts\"); while (have_posts()) : the_post(); ?> <div class=\"header-promo\">