m (Added Japanese interlanguage link) |
(→Excerpts vs. Full Posts) |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 2: | Line 2: | ||
{{en|Category_Templates}} | {{en|Category_Templates}} | ||
{{ja|Category_Templates}} | {{ja|Category_Templates}} | ||
+ | {{zh-cn|模板类别}} | ||
}} | }} | ||
Line 9: | Line 10: | ||
This article explains how to change what happens when the blog viewer is visiting one of your site's Category pages. This involves the use of Themes and Template files, so if you are new to template files, you might want to read [[Using Themes]] and [[Stepping Into Templates]] first. | This article explains how to change what happens when the blog viewer is visiting one of your site's Category pages. This involves the use of Themes and Template files, so if you are new to template files, you might want to read [[Using Themes]] and [[Stepping Into Templates]] first. | ||
− | Permalinks to category archives are controlled [[Using_Permalinks#Category_base_and_Tag_base|Using Permalinks]] settings. | + | Permalinks to category archives are controlled by [[Using_Permalinks#Category_base_and_Tag_base|Using Permalinks]] settings. |
== What Template File is Used? == | == What Template File is Used? == | ||
Line 24: | Line 25: | ||
That is, if you do not have a <tt style="font-weight:bold; color:#036">category-slug.php</tt> (lets say <tt style="font-weight:bold; color:#036">category-news.php</tt>), WordPress will check for a <tt style="font-weight:bold; color:#036">category-ID.php</tt> (like <tt style="font-weight:bold; color:#036">category-6.php</tt>), and so on. | That is, if you do not have a <tt style="font-weight:bold; color:#036">category-slug.php</tt> (lets say <tt style="font-weight:bold; color:#036">category-news.php</tt>), WordPress will check for a <tt style="font-weight:bold; color:#036">category-ID.php</tt> (like <tt style="font-weight:bold; color:#036">category-6.php</tt>), and so on. | ||
− | So, if you want to make the Category whose ID number is 6 look different from what it is currently (and different from other Category pages), you would want to create a <tt>category-6.php</tt> file. If you want to make all Category pages look different from other archive pages (such as date and author archives), then you would want to create or modify the <tt>category.php | + | So, if you want to make the Category whose ID number is 6 look different from what it is currently (and different from other Category pages), you would want to create a <tt>category-6.php</tt> file. If you want to make all Category pages look different from other archive pages (such as date and author archives), then you would want to create or modify the <tt>category.php</tt> file. If you want to make changes to the look of all archive pages, you can create or modify the <tt>archive.php</tt> file. And if you modify the <tt>index.php</tt> file, you will affect your entire blog. |
If you need to create a new file, it is a good idea to copy it from the next file in the hierarchy that exists. For instance, if you want a special display for Category 6, begin by copying the <tt>category.php</tt> file, or if you don't have one, use <tt>archive.php</tt>, and so on. | If you need to create a new file, it is a good idea to copy it from the next file in the hierarchy that exists. For instance, if you want a special display for Category 6, begin by copying the <tt>category.php</tt> file, or if you don't have one, use <tt>archive.php</tt>, and so on. | ||
Line 89: | Line 90: | ||
<pre> | <pre> | ||
<?php the_excerpt(); ?> | <?php the_excerpt(); ?> | ||
+ | </pre> | ||
and | and | ||
+ | <pre> | ||
<?php the_content(); ?> | <?php the_content(); ?> | ||
</pre> | </pre> |
Languages: English • 日本語 中文(简体) • (Add your language)
When a viewer clicks on a link to one of the Categories on your site, he or she is taken to a page listing the Posts in that particular Category in chronological order, from newest Posts at the top to oldest at the bottom. There are many display choices, including whether to display the complete post or post excerpts, and what additional information to display (title, author, publish date, last modified time, etc.). Each theme makes different choices, and you might want to change them.
This article explains how to change what happens when the blog viewer is visiting one of your site's Category pages. This involves the use of Themes and Template files, so if you are new to template files, you might want to read Using Themes and Stepping Into Templates first.
Permalinks to category archives are controlled by Using Permalinks settings.
The first step in modifying what happens when someone visits a Category page is to figure out which of your theme's files is going to be used to display the posts. This is known as the Template Hierarchy.
In the case of categories, the hierarchy is fairly simple. For instance, suppose the slug of the Category in question is news and the Category ID is 6. The Template Hierarchy specifies that WordPress will use the first Template file it finds in your current Theme's directory from the following list:
That is, if you do not have a category-slug.php (lets say category-news.php), WordPress will check for a category-ID.php (like category-6.php), and so on.
So, if you want to make the Category whose ID number is 6 look different from what it is currently (and different from other Category pages), you would want to create a category-6.php file. If you want to make all Category pages look different from other archive pages (such as date and author archives), then you would want to create or modify the category.php file. If you want to make changes to the look of all archive pages, you can create or modify the archive.php file. And if you modify the index.php file, you will affect your entire blog.
If you need to create a new file, it is a good idea to copy it from the next file in the hierarchy that exists. For instance, if you want a special display for Category 6, begin by copying the category.php file, or if you don't have one, use archive.php, and so on.
Now that you've figured out which template file in your theme's directory you need to modify, in order to make changes to the look of Category pages, let's look at some examples. In these examples, when it says "edit your template file", it means to edit the file you chose in the section above.
Suppose you want some static text displayed before the list of Posts on your Category page(s). By "static", we mean text that remains the same, no matter which posts will be displayed below, and no matter which category is being displayed. Here is how to do it: 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>
A slightly more complex possibility is that you want different text to display depending on which category page the visitor is viewing. Then you would add the "default" text to the main category.php file, and create special category-#.php files (with their own version of the text, as described in the Introduction) for each category that needs special text at the top.
This does however create a lot of files in your theme directory, and can be avoided using the following code OUTSIDE 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 does the following. Checks to see if we are looking at Category A, if we are then show the first bit of text, but if we're not then check if we are looking at Category B. If we are then show that bit of text, and finally, if it is neither Category A or B, then show this default text.
Another thing that can happen is that if your Category contains more posts than will fit on one page (according to the Options for Blog Reading you have set in the Administration panels of your blog), the category archive will split into multiple pages. And maybe you only want to display your static text if the viewer is on the first page of results, or you want to display different text for the other pages.
To make this happen, you can use a PHP if statement, which looks at the value of the $paged WordPress variable ($paged is equal to the page number: 1 for the first page of results, 2 for the second page, etc.). It may sound complicated, but it's actually not too bad. Just put the following above The Loop:
<?php if ( $paged < 2 ) : ?> <p>Text for first page of Category archive.</p> <?php else : ?> <p>Text for subsequent pages of Category. Can be left out.</p> <?php endif; ?>
Another possibility is to put the category name at the top of the page. If this is not already part of your template, you can add it by doing something like this, above The Loop:
<p>Category: <?php single_cat_title(); ?></p>
Perhaps you are looking to cut down on the size of your Category pages. You could do this by displaying excerpts rather than the entire content of each Post. To do this, you will just need to find where it says the_content() inside The Loop in your Template, and replace it with the_excerpt(). These will most likely be inside PHP tags:
<?php the_excerpt(); ?>
and
<?php the_content(); ?>
Conversely, if your Theme is currently displaying excerpts and you want full posts, replace the_excerpt with the_content.
Another thing that is interesting to do in a category archive page is to replace the post content with an image that links to your post. To do this, you will first need to put images into the Excerpt sections of your posts. Here's how to do that, for one post:
Now you will need to modify your template. We'll use a trick: the the_excerpt_rss() Template Tag does not put a paragraph tag around the excerpt. So we can use it to insert the img HTML and put it inside a link. Here's what you need to put into your Template, in place of using the_content:
<a href="<?php the_permalink() ?>"> <?php the_excerpt_rss(); ?> </a>
Caveat: using the excerpt this way may affect your RSS feed, because it places an img tag in the excerpt, instead of text. So if you are going to do this, you probably want to set your options so that the full posts are put in RSS feeds, rather than excerpts.
To select which category you would like to see on any page, for instance an archive page, use this.
<?php $args = array ( 'category' => ID, 'posts_per_page' => 5); $myposts = get_posts( $args ); foreach( $myposts as $post ) : setup_postdata($post); ?> //Style Posts here <?php endforeach; ?>
This uses the get_posts template tag in order to call an array of posts that adhere to the criteria that you set. In this instance, make sure you change "ID" to the category ID number you wish to show. The posts_per_page parameter simply states how many posts you would like to display from this category. We have set the number here to 5.
You can also use Conditional_Tags in order to create archives that respond to conditions set by the URL or context. For instance, if you would like to ensure that when a user visits http://yourblog.com/2008/02/ it will show posts from February of 2008 first, simply add this code to your archive.php file.
<?php if (have_posts()) : ?> <?php if (is_month()){ $year = get_the_time('Y'); $month = get_the_time('m'); $the_query = new WP_Query( 'monthnum=$month&year=$year&$cat=5' ); // The Loop while ( $the_query->have_posts() ) : $the_query->the_post(); echo '<li>' . get_the_title() . '</li>'; endwhile; /* Restore original Post Data * NB: Because we are using new WP_Query we aren't stomping on the * original $wp_query and it does not need to be reset. */ wp_reset_postdata(); ?>
Template Hierarchy: Category Templates, Tag Templates, Taxonomy Templates, Page Templates, Post Type Templates, Author Templates, Date Templates, Search Templates, 404 Templates, Attachment Templates, Loop Templates