WP Smith

Creating WordPress & Genesis Websites Since 2010

  • Home
  • About
  • Services
  • Blog
  • Contact

Jun 08 2011

Displaying Custom Post Types

Understanding WordPress Custom Post Types

There are several ways to display your custom post types:

  1. Display Custom Post Types with Blog Posts
  2. Display Custom Post Types with Custom Loop
  3. Display Custom Post Types via Shortcode

Display Custom Post Types with Posts

To display them with posts on the blog page, you can add this bit of code to your functions.php file:

[php]
add_filter( 'pre_get_posts' , 'wps_add_my_cpt' );
function wps_add_my_cpt() {
if ( is_home() && false == $query->query_vars['suppress_filters'] ) //if you want it to appear in the feed, comment this line & use the next line
if ( ( is_home() && false == $query->query_vars['suppress_filters'] ) || is_feed() )
$query->set( 'post_type' , 'array( 'post' , 'wps_cars' );
return $query;
}[/php]

Display Custom Post Types with Custom Loop

WordPress added support for single-type-template in Version 3.0 and for archive-type-template in Version 3.1.

Single template

In the form of the single-type-template. In the same way that posts are shown on their own page with single.php, custom post types will use single-{posttype}.php if it's available.

So for the above example, you could create a single-wps_cars.php file and the product posts would be shown using that template.

Archive template

In the form of the archive-type-template. In the same way that posts are shown on their own archive with archive.php, custom post types will use archive-{posttype}.php if it's available.

So for the above example, you could create a archive-wps_cars.php file and the product posts would be shown using that template.

*Note: Use the is_post_type_archive() function to check if the query shows post type archive page, and the post_type_archive_title() to echo the post type title.

Custom Post Type Custom Loops

To create your own custom loop in your page template (or, for Genesis users, you can see my post, <a href="https://wpsmith.net/frameworks/how-to-create-a-wordpress-custom-post-type-template-in-genesis/" target="_blank">How to Create a WordPress Custom Post Type Template in Genesis</a>), you can add this bit of code:

[php]$args = array( 'post_type' => 'wps_cars' , 'posts_per_page' => 5 , 'orderby' => 'post_title' , 'order' => 'ASC' );
$carsloop = new WP_Query( $args );
if ( $carsloop->have_posts() ) {
$output = '<div id="cars-content">';
while ( $carsloop ->have_posts() ) : $carsloop ->the_post();
$output .= the_title();
$output .= '<div class="entry-content">';
$output .= the_content();
$output .= '</div>';
endwhile;
$output .= '</div>';
}
else
$output = '<p>No cars were found.</p>';
[/php]

Display Custom Post Types via Shortcode

One way to display custom post types is to create a shortcode (thanks to Mark Jaquith, @markjaquith) to display them on pages or posts. There are a variety of reasons to use them and a variety of reasons that these can be abused. However, the benefits outweigh the risks and if done well, this can improve the user experience.

To display the cars custom post type that we have been working with, simply create a page and insert [ cars] (without the space in the front) to display wps_cars posts as a posts page (except we are ordering by Post Title in ascending order).

[php]function wps_cars_posting() {
global $post;
$xglobals = $GLOBALS;
$args = array( 'post_type' => 'wps_cars' , 'posts_per_page' => 5 , 'orderby' => 'post_title' , 'order' => 'ASC' );
$carsloop = new WP_Query( $args );
$output = '';
if ( $carsloop->have_posts() ) {
while ( $carsloop->have_posts() ): $carsloop->the_post();
$output .= '<div id="cars-content">';
$output .= get_the_post_thumbnail( $post->ID, 'thumbnail' );
$output .= '<strong>';
$output .= get_the_title();
$output .= '</strong>';
if (!empty( $post->post_excerpt ) )
$output .= '<em>'.$post->post_excerpt.'</em>';
$output .= get_the_content();
$output .= '</div>';
endwhile;
}
else
$output = '<p>No cars were found.</p>';
$GLOBALS = $xglobals;
return $output;
}

function my_init() {
add_shortcode( 'cars' , 'wps_cars_posting' );
}

add_action('init' , 'my_init');
[/php]

Be sure to add CSS your style.css accordingly.

For Genesis users, you can use the custom_loop function.

Written by Travis Smith · Categorized: Custom Post Types

StudioPress Premium WordPress Themes     WP Engine Managed WordPress Hosting

What can I do for you!?

Custom Development

We develop plugins by determining both business/functional and technical requirements, following WordPress development best practices, and using agile methodology to ensure you get the best solution.

Consulting

Have questions? Need a reliable developer to consult? Please contact us today!

Customized Theme

We can customize your theme or child theme, or create a child theme for you based on your needs while enhancing the performance of every individual attribute.

Customized Plugin

We can customize your plugins, extend plugins (e.g., Gravity Forms, Jetpack, Soliloquy) based on your needs ensuring security, performance, and positive business impact.

Contact Us

About Travis Smith

As a WordPress enthusiast, developer, and speaker, Travis writes about what he learns in WordPress trying to help other WordPress travelers, beginners and enthusiasts with tutorials, explanations, & demonstrations.

Comments

  1. Damien says

    June 16, 2011 at 5:36 am

    I’m using standard categories and tags with a custom post type to display video posts. Is it possible to display custom post types and standard posts in a category or tag archive?

    Reply
  2. Hanè says

    September 1, 2011 at 7:52 am

    Thanks for the info, I was wondering if there is away to add a shortcode for the archive page?

    Thanks
    Hanè

    Reply
    • Travis Smith says

      September 1, 2011 at 8:54 am

      Hello Hanè,

      I am not sure what you are asking. The short answer is yes, but I would need more information to understand what you are requesting.

      Thanks,

      Reply
  3. Pipeline says

    April 2, 2012 at 5:26 pm

    Hi, im using genesis 1.8 with agentpress and im trying to call via the genesis_post_content hook the shortcode [property_details] inside every listing created by user using gravity forms. But if i use just the shortcode it is called on every single post. What code should i insert into the hook (im using genesis simple hooks plugin) to test if post type is “listing” and display the property_details, and prevent it to showing up in blog posts and pages? Tried different ways but didnt make it… Thanks in advance for the help.

    Reply
    • Travis Smith says

      May 13, 2012 at 8:01 pm

      Hello Pipeline, you need something like this inside your function:
      [php]
      global $post;
      if ( ‘listing’ == $post->post_type ) {
      //execute code
      }[/php]

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Need Help?

Please let us know how we can help you!

Get Help

Recommendations

Genesis WordPress Framework
Sucuri Security
Gravity Forms
GetSoliloquy
Get Envira
Scribe SEO
BackupBuddy
WordPress Video User Manuals

Recent Posts

  • Solving WordPress 5XX Server Errors on SiteGround
  • Hiding an User in the WordPress Admin
  • Custom Rewrite Rules for Custom Post Types and Taxonomies
  • WordPress JavaScript Manager Native Functions
  • Causes of WordPress Site Performance Slowdown
  • Twitter
  • Facebook
  • LinkedIn
  • Google+
  • RSS

Copyright © 2025 � WP Smith on Genesis on Genesis Framework � WordPress � Log in