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.