开发者

Apply wordpress Template of category to posts

I want a post have the template of the parent category.. Is that possible? if yes, please guide me a bit. Or i开发者_高级运维f any plugin is available, name it.


As of Wordpress 3.0, the logic in wp-includes/template-loader.php for selecting a template looks like this:

if ( defined('WP_USE_THEMES') && WP_USE_THEMES ) :
    $template = false;
    if     ( is_404()            && $template = get_404_template()            ) :
    elseif ( is_search()         && $template = get_search_template()         ) :
    elseif ( is_tax()            && $template = get_taxonomy_template()       ) :
    elseif ( is_front_page()     && $template = get_front_page_template()     ) :
    elseif ( is_home()           && $template = get_home_template()           ) :
    elseif ( is_attachment()     && $template = get_attachment_template()     ) :
        remove_filter('the_content', 'prepend_attachment');
    elseif ( is_single()         && $template = get_single_template()         ) :
    elseif ( is_page()           && $template = get_page_template()           ) :
    elseif ( is_category()       && $template = get_category_template()       ) :
    elseif ( is_tag()            && $template = get_tag_template()            ) :
    elseif ( is_author()         && $template = get_author_template()         ) :
    elseif ( is_date()           && $template = get_date_template()           ) :
    elseif ( is_archive()        && $template = get_archive_template()        ) :
    elseif ( is_comments_popup() && $template = get_comments_popup_template() ) :
    elseif ( is_paged()          && $template = get_paged_template()          ) :
    else :
        $template = get_index_template();
    endif;
    if ( $template = apply_filters( 'template_include', $template ) )
        include( $template );
    return;
endif;

Checking get_category_template() in wp-includes/theme.php` we see:

function get_category_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";
    $templates[] = "category.php";

    $template = locate_template($templates);
    return apply_filters('category_template', $template);
}

Assuming that your category is Foo, that it's slug is foo, and that the Foo category ID is 17, for a post that belongs to category Foo, Wordpress will check for the following templates in your theme and use the first one it finds:

  • category-foo.php
  • category-17.php
  • category.php

Thus, all you should need to do is to create a template named category-foo.php in your theme directory, and set your post's category to Foo, and that post will be rendered using the category-foo.php template instead of the default post.php template.

This mechanism for selecting templates has been present since Wordpress 1.5, though full list of template types has grown significantly over the years.

The Wordpress documentation for this can be found here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜