Display Different style templates for different categories of wordpress themes

Source: Internet
Author: User
Tags php file

The following is the specific implementation code. Take it away if you need it. The code is ready for use by myself...

The code is as follows: Copy code

<? Php
If (in_category ('post ')){
Include (TEMPLATEPATH. '/single-post.php ');
}
// Elseif, in one judgment, tries to add judgment to copy the code
Elseif (in_category ('plugin') {// pro is the alias of category
Include (TEMPLATEPATH. '/single-plugin.php ');
}
// Elseif ends
Else {
Include (TEMPLATEPATH. '/single-all.php ');
}
?>

The post in in_category ('post') also supports ID, that is, writing in_category ('3 & prime ;).

The other is similar. We can directly modify the style.

IF IN_CATEGORY condition label
First, copy the two single. Php files named "single1.php" and "single2.php" respectively ". Then, delete all the content in the original single. Php file and replace it with the following code:

The code is as follows: Copy code

<? Php
$ Post = $ wp_query-> post;
If (in_category ('9 & prime ;)){
Include (TEMPLATEPATH. '/single2.php ');
} Else {
Include (TEMPLATEPATH. '/single1.php ');
}
?>

Check the log. If the log belongs to the category ID9, single2.php is displayed. If not, single1.php is displayed.

You can also use more condition statements to specify different styles and content for different classes. For example:

The code is as follows: Copy code

<? Php
$ Post = $ wp_query-> post;
If (in_category ('9 & prime ;)){
Include (TEMPLATEPATH. '/single9.php ');
Elseif (in_category ('12 & prime ;)){
Include (TEMPLATEPATH. '/single12.php ');
Elseif (in_category ('42 & prime ;){
Include (TEMPLATEPATH. '/single42.php ');
} Else {
Include (TEMPLATEPATH. '/single1.php ');
}}
?>

Because I don't want to change these two single template files, but just add an extra style table to the second file single2.php, then I create two header template files, just like copying single. same as php. Then add the link to the external style sheet at the top of the second header file:

The code is as follows: Copy code

<Style type = "text/css" media = "screen">
@ Import URL ('/wp-content/themes/mytheme/style.css ');
@ Import URL ('/wp-content/themes/mytheme/cssstyles.css ');
</Style>

Next, we need to call header2.php in the second single2.php:

The code is as follows: Copy code

<? Php
/* Don't remove this line .*/
Require ('./wp-blog-header.php ');
Include (get_template_directory (). '/header2.php'
?>

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.