WordPress add category text

WordPress add category text

Your posts are filed in different categories. Category information is freqently displayed in the post meta data section near your post or under the heading title. Different WordPress Themes highlight the post meta data section in different areas.

The display of the post categories is generated through the use of the the_category() template tag. And you have the ability to style how these categories are displayed.

Finding Your Categories Tag

The placement of your categories tag may be in one place on the front page and in one or more different places in your single post page, so you may have to do some hunting to find your various categories tags. You might also want to style one differently from the others, but you still have to find them.

The post meta data featuring your categories tag in your Theme is usually found on the index.php, single.php, or sometimes in the sidebar.php template files. Open one or more of these template files and search for:

Once you have found it, take another look at a generated web page of your site and determine how exactly you might want to change this information.

The the_category() template tag instructs the database to get the information regarding the post categories and display it at that point in your template file. By default, it displays the list of categories with a space between each one. You can change this by adding the parameter inside of the tag. Let’s begin with simple separators by playing with the category names: WordPress, Computers, and Internet News.

Simple Separators

If you would like to have commas between the categories, the tag should read:

And it would look like this:

If you would like to have an arrow, the tag would look like this:

If you would like to have a bullet, the tag would look like this:

If you would like the «pipe» ( | ) between the categories, the tag would look like this:

Adding Text to Your Categories

Would you like to make your post meta data look a little more textual, informal and part of a paragraph rather than a list. You can add an «and» to go between the categories like this:

Or you can give them more of a choice and change the «and» to an «or»:

The possibilities are endless. Have fun and use your imagination!

Источник

Introduction

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.

What Template File is Used?

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.

Читайте также:  Как разобрать принтер эпсон р270

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.

Examples

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.

Adding Text to Category Pages

Static Text Above Posts

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:

Different Text on Some Category Pages

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:

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.

Text Displaying Only on First Page of Archive

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:

Category Name

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:

Modifying How Posts are Displayed

Excerpts vs. Full Posts

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:

Читайте также:  Картридж лазерный nv print c7115a

Conversely, if your Theme is currently displaying excerpts and you want full posts, replace the_excerpt with the_content.

Display Images Linking to Full Posts

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:

  1. Upload an image on the post editing screen.
  2. Switch to the «Code» editor, rather than the «Visual» editor.
  3. Use the Uploads / Browse tab to insert the image into your post. Make sure to insert the full-sized image, with no link.
  4. Copy the inserted HTML img tag, and paste it into the «Optional Excerpt» section of the post editing screen.
  5. Finish writing the post content (you can remove the image), and publish the 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:

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.

What categories do you show to the visitors?

To select which category you would like to see on any page, for instance an archive page, use this.

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.

Источник

Developer Resources

Chapters

Taxonomy Templates

Topics

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.

Taxonomy Template Hierarchy

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.

Category

For categories, WordPress looks for the category-.php file. If it doesn’t exist, WordPress then looks for a file for the next hierarchical level, category-.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:

  1. category-.php: For example, if the category’s slug is named “news,” WordPress would look for a file named category-news.php.
  2. category-.php: For example, if the category’s ID is “6”, WordPress would look for a file named category-6.php.
  3. category.php
  4. archive.php
  5. index.php

For tags, WordPress looks for the tag-.php file. If it doesn’t exist, WordPress then looks for a file for the next hierarchical level, tag-.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:

  1. tag-.php: For example, if the tag’s slug is named “sometag,” WordPress would look for a file named tag-sometag.php.
  2. tag-.php: For example, if the tag’s ID were “6,” WordPress would look for a file named tag-6.php.
  3. tag.php
  4. archive.php
  5. index.php

Custom Taxonomy

A custom taxonomy hierarchy works similarly to the categories and tags hierarchies described above. WordPress looks for the taxonomy-.php file. If it doesn’t exist, WordPress then looks for a file for the next hierarchical level, 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:

  1. taxonomy-.php: For example, if the taxonomy is named “sometax,” and the taxonomy’s term is “someterm,” WordPress would look for a file named taxonomy-sometax-someterm.php.
  2. taxonomy-.php: For example, if the taxonomy is named “sometax,” WordPress would look for a file named taxonomy-sometax.php
  3. taxonomy.php
  4. archive.php
  5. index.php

Creating Taxonomy Template Files

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.

Examples

Now that you’ve selected the template file in your theme’s directory that you need to modify, let’s look at some examples.

Adding Text to Category Pages

Static Text Above Posts

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:

This text will only display on an archive page displaying posts in that category.

Different Text on Some Category Pages

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-.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:

This code does the following:

  1. Checks to see if the visitor has requested Category A. If yes, it displays the first piece of text.
  2. Checks for category B if the user didn’t request category A. If yes, it displays the second piece of text.
  3. Displays the default text, if neither was requested.

Display Text only on First Page of Archive

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:

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.

Modify How Posts are Displayed

Excerpts vs. Full Posts

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:

And replace it with:

And if your theme is displaying excerpts but you want to display the full content, replace the_excerpt with the_content .

Источник

Поделиться с друзьями
КомпСовет
Adblock
detector