本文档介绍了 WordPress 中分类法模板(如分类、标签和自定义分类法)的工作原理和创建方法。通过模板层次结构,开发者可以定制不同分类法页面的显示方式,覆盖默认的 index.php 模板。
<?php if ( is_category( 'Category A' ) ) : ?>
<p>This is the text to describe category A.</p>
<?php elseif ( is_category( 'Category B' ) ) : ?>
<p>This is the text to describe category B.</p>
<?php else : ?>
<p>This is some generic text to describe all other category pages, I could be left blank.</p>
<?php endif; ?>When a visitor clicks on a hyperlink to category, tag or custom taxonomy, WordPress displays a page of posts in reverse chronological order filtered by that taxonomy.
By default, this page is generated using the index.php template file. You can create optional template files to override and refine the index.php template files. This section explains how to use and create such templates.
WordPress display posts in the order determined by the Template Hierarchy.
The category.php, tag.php, and taxonomy.php templates allow posts filtered by taxonomy to be treated differently from unfiltered posts or posts filtered by a different taxonomy. (Note: post refers to any post type – posts, pages, custom post types, etc.). These files let you target specific taxonomies or specific taxonomy terms. For example:
So you could format all posts in an animal taxonomy named news on a page that looks different from posts filtered in other categories.
The archive.php template provides the most general form of control, providing a layout for all archives; that is, a page that displays a list of posts.
For categories, WordPress looks for the category-{slug}.php file. If it doesn’t exist, WordPress then looks for a file for the next hierarchical level, category-{ID}.php, and so on. If WordPress fails to find any specialized templates or an archive.php template file, it reverts to the default behavior, using index.php.
The category hierarchy is listed below:
For tags, WordPress looks for the tag-{slug}.php file. If it doesn’t exist, WordPress then looks for a file for the next hierarchical level, tag-{ID}.php, and so on. If WordPress fails to find any specialized templates or an archive.php template file, it will revert to the default behavior, using index.php.
The tag hierarchy is listed below:
A custom taxonomy hierarchy works similarly to the categories and tags hierarchies described above. WordPress looks for the taxonomy-{taxonomy}-{term}.php file. If it doesn’t exist, WordPress then looks for a file for the next hierarchical level, taxonomy-{taxonomy}.php, and so on. If WordPress fails to find any specialized templates or an archive.php template file, it will revert to the default behavior, using index.php.
The hierarchy for a custom taxonomy is listed below:
Now you’ve decided that you need to create custom designs for content based on taxonomies, where do you start?
Rather than starting from a blank file, it is good practice to copy the next file in the hierarchy, if it exists. If you’ve already created an archive.php, make a copy called category.php and modify that to suit your design needs. If you don’t have an archive.php file, use a copy of your theme’s index.php as a starting point.
Follow the same procedure if you are creating any taxonomy template file. Use a copy of your archive.php, category.php, tag.php, or index.php as a starting point.
Now that you’ve selected the template file in your theme’s directory that you need to modify, let’s look at some examples.
Suppose you want some static text displayed before the list of posts on your category page(s). “Static” is text that remains the same, no matter which posts are displayed below, and no matter which category is displayed.
Open your file and above The Loop section of your Template file, insert the following code:
<p>This is some text that will display at the top of the Category page.</p>
This text will only display on an archive page displaying posts in that category.
What if you want to display different text based on the category page that the visitor is using? You could add default text to the main category.php file, and create special category-{slug}.php files each with their own version of the text, but this would create lots of files in your theme. Instead, you can use conditional tags.
Again, this code would be added before the loop:
<?php if ( is_category( 'Category A' ) ) : ?>
<p>This is the text to describe category A.</p>
<?php elseif ( is_category( 'Category B' ) ) : ?>
<p>This is the text to describe category B.</p>
<?php else : ?>
<p>This is some generic text to describe all other category pages, I could be left blank.</p>
<?php endif; ?>
This code does the following:
If you have more posts than fits on one page of your archive, the category splits into multiple pages. Perhaps you want to display static text, if the user is on the first page of the results.
To do this, use a PHP if statement that looks at the value of the $paged WordPress variable.
Put the following above The Loop:
<?php if ( $paged < 2 ) : ?>
<p>Text for first page of Category archive.</p>
<?php endif; ?>
This code asks whether the page displayed is the first page of the archive. If it is, the text for the first page is displayed. Otherwise, the text for the subsequent pages is displayed.
You can choose whether to display full posts or just excerpts. By displaying excerpts, you shorten the length of your archive page.
Open your file and find the loop. Look for:
the_content()
And replace it with:
the_excerpt()
And if your theme is displaying excerpts but you want to display the full content, replace the_excerpt with the_content.