Tuesday, August 28, 2012

WordPress Post Types and Taxonomies – Displaying Post Types

Php programming

Php programming (Photo credit: Wikipedia)



Since WordPress post types are simply an extension of the existing classification system, displaying them in a theme is quite similar to what is already in place. There are currently three primary ways to display custom post types in your themes:



  • Post Query

  • Single Post Template

  • Archive Template


Displaying via Post Query


To display the new post type mysite_reviews, you will want to open up the template file that you would like to display it on (in my case, I usually create a custom home.phpfor templates), and enter the following code:



$args = array( 'post_type' => 'mysite_reviews', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<div class="entry-content">';
the_excerpt();
echo '</div>';
endwhile;

This simply creates a new WordPress loop that will display the title and excerpt from the 10 most recent entries in the mysite_reviews post type.


Displaying via Single Post Template


Just as you can customize the way individual posts are displayed via a theme’s single.php file, you can customize the way your individual post type entries are displayed. The easiest way to do this is to create a duplicate of your theme’s single.php file and rename it to single-{posttypename}.php . From there, you can customize that file to your specs. Using the example from above, we would need a single post template named single-mysite_reviews.php.


Displaying by Archive Template


While this feature will not be available until WP 3.1 releases, post types can also be displayed archive-style by creating a file in your theme named archive-{posttypename}.php. So, if we were creating an archive for the post type, we would create an archive template named archive-mysite_reviews.php and place it within our template folder.


This is a part of WordPress Post Types and Taxonomies tutorial.



The post WordPress Post Types and Taxonomies – Displaying Post Types appeared first on Tutorial Mini.


Related posts:



  1. WordPress Post Types and Taxonomies

  2. WordPress Post Types and Taxonomies – Integrating Post Types

  3. WordPress Template Hierarchy – Template hierarchy in details






via Tutorial Mini http://tutorialmini.com/wordpress-post-types-and-taxonomies-displaying-post-types/

No comments:

Post a Comment