WordPress Display Custom Post Type

July 21, 2015

Alright! So you’ve figured out how to add a custom post type or just saved yourself the trouble and installed Custom Post Type UI and now you’re looking for a way to display them.

Check out the code below:

<?php
/**
 * Template Name: Videos
 *
 * Selectable from a dropdown menu on the edit page screen.
 */

get_header(); ?>

<div class="primary">
    <main id="main" class="site-main" role="main">

<?php
    $args = array(
        'post_type' => array('video')
    );

    // the query
    $the_query = new WP_Query( $args );
?>

<?php if ( $the_query->have_posts() ) : ?>

    <!-- pagination here -->

    <!-- the loop -->
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <h1 class="entry-title">
            <a href="<?php the_permalink(); ?>">
                <?php the_title(); ?>
            </a>
        </h1>
    <?php endwhile; ?>
    <!-- end of the loop -->

    <!-- pagination here -->

    <?php wp_reset_postdata(); ?>

    <?php else : ?>
        <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>

    </main><!-- #main -->
</div><!-- #content -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>