WordPress different categories call different templates

Source: Internet
Author: User

This refers to the default article type template (single.php,category.php)

Application Scenarios:

Default article default has 2 categories (news, game information)

all sub-categories under news call " News list template , news content Template "

all sub-categories under game profile call " game Data list template , game profile content Template "

Article List page category.php

Judge in category.php.

If the subcategory belongs to the news root category, the News list template is called

If the subcategory belongs to the game data root category, the game Profile list template is called

The key here is "to determine if the sub-classification is a function of the root classification "

WordPress does not have a default function and requires the following code:

 //function Cate_is_in_descendant_category ($cats)
parameter $cats A category ID, multiple categories with ID array

If( !function_exists("Post_is_is_descendant_category" ) ) { function cate_is_in_descendant_category($cats) { foreach( (Array)$cats as $cat ) { //Get_term_children () accepts integer ID only $descendants= Get_term_children ((int)$cat, "category" ); if($descendants&&is_category($descendants ) ) return true; } return false; }}

is_category ( $category )
Parameters: $category

Mixed (optional) Category ID, category header title, category short tag Slug or ID array, title array, slugs array.
Default: None

Implementing operations

First, copy the two category.php files named "category1.php" and "category2.php" respectively.
Then, delete all the contents of the original category.php file and replace it with the following code:

<? PHP if  ) {    include(TemplatePath). '/category1.  Else  {    include(TemplatePath. '/category2. php ');}
?>


This means: Check the category page ID, if the ID belongs to category ID9, display category1.php, if not, display category2.php.

Article List page category.php

judge in single.php.

The key here is "to determine whether an article under sub-classification is a function of the root classification "

WordPress does not have a default function and requires the following code:

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; }}

in_category( $category , $_post )
Parameter 1: $category

(mixed) (required) One or more of the specified category IDs, categorical aliases or slug, or an array.

Default: None

Parameter 2:$_post

(mixed) (optional) article, which defaults to the current article within the main loop or to the article in the main query.

Default: None

Implementing operations

First, copy the two single.php files named "single1.php" and "single2.php" respectively.
Then, delete all the contents of the original single.php file and replace it with the following code:

<? phpif (Cate_is_in_descendant_category (2) ) {    include (TemplatePath. '/single1. php ');} Else {    include (TemplatePath. '/single2. php ');}
?>


Check the log, if the log belongs to the classification ID9, then display single1.php, if not, then display single2.php.

WordPress different categories call different templates

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.