Wordpress Display Custom post types

sammyukavi picture sammyukavi · Sep 20, 2012 · Viewed 41k times · Source

I'm a newbie in WordPress. I have created a custom post type for videos but I do not know how to display the post type in a page. For example I want when a user adds a video, he won't have to select a video template when publishing the video and when they open the published video, the page opens up with a video player instead of opening a page. I want something like a custom page with the video player ready that all I have to do is give the video player the url for the video.Already have the code for the video player. How can I do this?

Answer

Cody MacPixelface picture Cody MacPixelface · Sep 20, 2012

To make a default template file for all your custom post type posts or pages, you can name your template file single-{your-cpt-name-here}.php or archive-{your-cpt-name-here}.php and it will always default to this when viewing these posts or pages.

So for example in single-video.php you could put:

<?php query_posts( 'post_type=my_post_type' ); ?>

or instead make a custom query to shape your desired output:

<?php
$args = array(
    'post_type' => 'my_post_type',
    'post_status' => 'publish',
    'posts_per_page' => -1
);
$posts = new WP_Query( $args );
if ( $posts -> have_posts() ) {
    while ( $posts -> have_posts() ) {

        the_content();
        // Or your video player code here

    }
}
wp_reset_query();
?>

In your custom loop like the example above, there's lots of available template tags (like the_content) to choose from in Wordpress.