Examples of using different document template styles under the WordPress custom category menu

Source: Internet
Author: User
Tags php file

First of all, the tribe adds that different article template styles mentioned here do not require two topics, but only one topic is required.

1. Add the following code to the functions. php file of the current topic:

The code is as follows: Copy code
// Define the Directory of the template file as the single folder
Define (SINGLE_PATH, TEMPLATEPATH .'/');
// Automatically select the template function
Function svr_single_template ($ single ){
Global $ wp_query, $ post;
// Select a template file by category alias or ID
Foreach (array) get_the_category () as $ cat ):
If (file_exists (SINGLE_PATH. '/single-'. $ cat-> slug. '. Php '))
Return SINGLE_PATH. '/single-'. $ cat-> slug. '. Php ';
Elseif (file_exists (SINGLE_PATH. '/single-'. $ cat-> term_id. '. Php '))
Return SINGLE_PATH. '/single-'. $ cat-> term_id. '. Php ';
Endforeach;
}  
// Mount the function through the single_template hook
Add_filter ('Single _ template', 'svr _ single_template ');

2. next, create a folder named single in the root directory of the current topic, and create different files according to different categories. The naming format is single-[category alias or ID]. php. Remember to create your own template file for all categories.

The main function of this function is to call the php file single-1 if the classification ID is 1.

For example, if the alias for classification is "themes", ID is 2, then it can be named single-themes.php or single-2.php. as a result, the single-themes.php or single-2.php is automatically selected as the article template for this category as long as it is the WordPress theme.

In addition, another method is to use in_category () for judgment, but there are a lot of changes to be made. Therefore, we recommend that you use the first method.

Summary

For those who make their own theme, you can consider this function to meet some special customer requirements.

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.