3
votes

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?

3

3 Answers

9
votes

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.

3
votes

Write Code In Functions.php

    function create_post_type() {
    register_post_type( 'Movies',
    array(
        'labels' => array(
            'name' => __( 'Movies' ),
            'singular_name' => __( 'Movie' )
        ),
        'public' => true,
        'has_archive' => true,
        'rewrite' => array('slug' => 'Movies'),
    )
);

Now Write This Code Where You Want To show

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

?>

1
votes

After you created the CPT, do this for showing single posts of your CPT:

  • Duplicate the single.php file in your template and rename it like single-{post_type}.php (eg. single-movie.php)
  • Remember to flush the permalinks from WordPress!

You can get more details from this post

  • Now if you want to show a list of CPT you can use get_posts() with args:

    $args = array( ... 'post_type' => 'movie' )

Check this post for further details.