动态创建Wordpress类别模板

时间:2012-05-25 09:58:58

标签: wordpress wordpress-plugin wordpress-theming categories

我想创建一个插件,当我创建子类别时,它将使用类别slug名称动态创建类别模板。

就像我有一个类别新闻。为此,我有一个模板“category-news.php”。现在我在名为“最新新闻”的新闻下创建一个新的子类别。因此,我想在我的主题文件夹中创建一个实用名为'category-latest-news.php'的文件,并将category-news.php模板代码放入其中。

2 个答案:

答案 0 :(得分:0)

您可以使用php的fopen和fwrite创建文件,然后以编程方式在模板顶部插入模板名称。

获取当前页面的父ID:

$parents = get_post_ancestors( $post->ID );
$parentsid = $parents[count($parents)-1];

然后您可以使用Make Wordpress subcategories use Category Template

中的代码
function myTemplateSelect() {
    if (is_category() && !is_feed()) {
        if (is_category(get_cat_id('projects')) || cat_is_ancestor_of(get_cat_id('projects'), get_query_var('cat'))) {
            load_template(TEMPLATEPATH . '/category-projects.php');
            exit;
        }
    }
}

add_action('template_redirect', 'myTemplateSelect');

答案 1 :(得分:0)

使用模板过滤器

add_filter( 'template_include', 'template_include', 10 );

并将模板更改为

function template_include($template)
{

    if(your condition here){
        $template = get_template_directory().'/your-template.php';
    }

    return $template;
}
相关问题