WordPress Content Types (Query Posts Tag)

WordPress makes it possible to pull in specific types of content on your website through the query_posts(); template tag. You place this template tag before The Loop, and it lets you specify which content types you want to pull information from.


If you have a WordPress category, for example, and you want to display the last three posts from that category on your front page, in a sidebar, or somewhere else on your site, you can use this template tag. You can also use the query_posts(); tag to pull content from individual pages.


Tag parameters


The query_posts template tag has several parameters that let you display different types of content, such as posts in specific categories, content from specific pages/posts, or dates in your blog archives. Here’s an example of two parameters 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, for example, enter showposts=3.



  • category_name=X: This parameter tells WordPress that you want to pull posts from the category with this specific slug. If the category slug is books-i-read, for example, enter category_name=books-i-read.




The parameter category_name is slightly misleading because you don’t use the category name, but rather the category slug, which is different.


The query_posts template tag lets you pass so many variables and parameters that it’s impossible to list all the possibilities, so let's look at how to use the tag to display content from a specific category only. To find out about all the possible combinations of parameters you can use for the query_posts tag, visit the WordPress Codex page and read about the options available with this tag.


Add the query posts tag


Choose which category you want to list posts from and locate the slug that belongs to the category. After you do that, you’re ready to add the query_posts tag to your template.


The category slug is usually the same as the category name, except in lowercase with words separated by dashes; for example, a Books I Read category has a books-i-read slug. To double-check, visit the Category page in your Dashboard by choosing Posts→Categories, click the name of the category you want to use, and find the category slug listed.


Follow these steps to add the query_posts tag to your template:



  1. In your Dashboard, choose Appearance→Editor.


    The Edit Themes page opens.



  2. In the Templates list on the right side of the page, click the template in which you want to display the content.


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


    The template you select appears in the editor in the middle of the page.



  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, for example, the ending </ul> tag is the second-to-the-last line.



  4. Type the following code directly above the ending </ul> tag:


    <?php query_posts('showposts=3&category_name=books-i-read); ?>
    <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; ?>


  5. Click the Update File button to save the changes to your template.




In the first line, the following was indicated: showposts=3&category_name=books-i-read. You can change these numbers to suit your specific needs. Just change 3 to whatever number of posts you want to display (there is no limit!), and change books-i-read to the specific category slug that you want to use.




dummies

Source:http://www.dummies.com/how-to/content/wordpress-content-types-query-posts-tag.html

No comments:

Post a Comment