Pulling in Content from a Single Category

WordPress makes it possible to pull in very specific types of content on your Web site through the <query_posts> template tag. If you place this template tag before The Loop (see Book VI), it lets you specify which category you want to pull information from. If you have a category called WordPress and you want to display the last three posts from that category — on your front page, in your blog sidebar, or somewhere else on your site — you can use this template tag.

image The <query_posts> template tag has several parameters that let you display different types of content, such as posts in specific categories and content from specific pages/posts or dates in your blog archives. The <query_posts> tag lets you pass so many variables and parameters that we just can't list all the possibilities. Instead, you can visit this page in the WordPress Codex and read about the options available with this tag: http://codex.wordpress.org/Template_Tags/query_posts.

First, you have to find the ID number for the category you want to use; then you have to tell WordPress how to display the content by using the <query_posts> tag.

Finding the category ID number

You can't easily find the unique ID number for a category unless you know where to look. To locate it, follow these steps:

  1. Click Categories in the Posts drop-down list.

    The Categories page opens.

  2. Hover your mouse over the name of the category for which you need the ID number.

    The category ID number appears in the status bar of your browser.

    Figure 6-5 shows the category ID in a browser's status bar. (Mozilla Firefox, Internet Explorer, and Safari have similar status bars.) The last part of the URL displayed in the status bar is tag_ID=1, which means that 1 is the ID for this Uncategorized category.

Adding the <query_post> tag

When you have the category ID number in hand, you're ready to add the <query_post> tag to your template.

Here are two parameters that you can use with the <query_posts> tag:

  • showposts=X: This parameter tells WordPress how many posts you want to display. If you want to display only three posts, enter showposts=3.
  • cat=X: This parameter tells WordPress that you want to pull posts from the category with this specific ID number. If the ID category is 40, enter cat=40.

    Figure 6-5: Finding the unique category ID number.

    image

Follow these steps to add the <query_post> tag to your template:

  1. Click Editor in the Appearance drop-down list.

    The Edit Themes page opens.

  2. Click the template in which you want to display the content.

    If you want to display content in a sidebar, for example, choose the Sidebar template: sidebar.php.

  3. Locate the ending </ul> tag at the bottom of the template for the theme you're using.

    If you're using the Twenty Ten theme, the ending </ul> tag is the second-to-last line.

  4. Type the following code directly above the ending </ul> tag:
    <?php query_posts('showposts=3&cat=40'), ?>
    <h2>Type Your Desired Title Here</h2>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <strong><a href="<?php the_permalink() ?>" rel="bookmark"
        title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_
        title(); ?></a></strong>
    <?php the_excerpt();endwhile;endif; ?>

    image In the first line, you indicate the following: showposts=3&cat=40. You can change these numbers to suit your specific needs. Just change 3 to whatever number of posts you want to display (there's no limit!) and change 40 to the specific category ID number that you want to use.

  5. Click the Update File button.

    The changes you just made are saved to the sidebar.php template.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.243.130