Display custom post type in template

I have a custom post type called campsites, events and activities

I want to display this in a list.

Read More

So when the user click on the link, it will then display all the posts under that custom post type.

Any help would be greatly appreciated.

Edit

add_action('init', 'activity_resources_register_post_type');

function activity_resources_register_post_type() {
    register_post_type('activity_resources', array(
        'labels' => array(
            'name' => 'Activity Resources',
            'singular_name' => 'Activity Resources',
            'add_new' => 'Add new Activity Resources',
            'edit_item' => 'Edit Activity Resources',
            'new_item' => 'New Activity Resources',
            'view_item' => 'View Activity Resources',
            'search_items' => 'Search Activity Resources',
            'not_found' => 'No Activity Resources found',
            'not_found_in_trash' => 'No Activity Resources found in Trash'
        ),
        'public' => true,
        'has_archive' => 'activity_resources',
        'rewrite' => array("slug" => "activity_resources"), // the slug for permalinks
        'supports' => array('title', 'editor', 'thumbnail')
    ));
}

Related posts

Leave a Reply

2 comments

  1. Create custom page template

    The first step is to create a custom page template to hold the code. For example, name the file template-cpt-list.php:

    <?php
    /**
     * Template Name: Custom Post Types List
     */
    
    get_header();
    
    // Custom code will go here
    
    get_footer();
    
    ?>
    

    Create list of generated Custom Post Types (CPTs)

    The next step is generating your list of CPTs. There’s a core function for that: get_post_types():

    <?php get_post_types( $args, $output, $operator ); ?>
    

    So, for example, to return public, custom post types by name:

    $custom_post_types = get_post_types( 
        array(
            // Set to FALSE to return only custom post types
            '_builtin' => false,
            // Set to TRUE to return only public post types
            'public' => true
        ),
        // Set to "objects", so we have the full CPT object
        'objects'
     );
    

    Now, your CPTs are contained in a variable that has an array of CPT objects.

    Create list of CPT archive index permalinks

    So the next step is to take the CPT objects, and use them to create a list of permalinks to each CPT archive index. There is also a core function for that, as well: get_post_type_archive_link():

    <?php get_post_type_archive_link( $posttype ); ?>
    

    So we just step trough our array of post type names, and retrieve the permalink for each:

    foreach ( $custom_post_types as $custom_post_type ) {
        $custom_post_type->permalink = get_post_type_archive_link( $custom_post_type->name );
    }
    

    Then you can use that array of permalinks (URLs) to create your list markup:

    <ul>
    <?php
    foreach ( $custom_post_types as $custom_post_type ) {
        echo '<li>';
        echo '<a href="' . $custom_post_type->permalink . '">' . $custom_post_type->name . '</a>';
        echo '</li>';
    }
    ?>
    </ul>
    

    Putting it all together

    Your template-cpt-list.php should now look like so:

    <?php
    /**
     * Template Name: Custom Post Types List
     */
    
    get_header();
    
    // Get list of CPTs
    $custom_post_types = get_post_types( 
        array(
            // Set to FALSE to return only custom post types
            '_builtin' => false,
            // Set to TRUE to return only public post types
            'public' => true
        ),
        // Set to "objects", so we have the full CPT object
        'objects'
     );
    
    // Add CPT permalinks to CPT objects
    foreach ( $custom_post_types as $custom_post_type ) {
        $custom_post_type->permalink = get_post_type_archive_link( $custom_post_type->name );
    }
    
    // Output CPT archive index permalinks list
    echo '<ul>';
    foreach ( $custom_post_types as $custom_post_type ) {
        echo '<li>';
        echo '<a href="' . $custom_post_type->permalink . '">' . $custom_post_type->name . '</a>';
        echo '</li>';
    }
    echo '</ul>';
    
    get_footer();
    
    ?>
    
  2. Mandy,

    You can create a new page template (name it what you want, I would name it ‘page-activity-resources.php’) that contains a WP_Query to pull your activity resources posts and display with pagination.

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $cpt_query_args = array(
    'post_type' => 'activity_resources',
    'posts_per_page' => 5,
    'paged' => $paged
    );
    $cpt_query = new WP_Query( $cpt_query_args );
    if ($cpt_query->have_posts()) : while ($cpt_query->have_posts()) : $cpt->the_post(); ?>
    

    To list out all of the custom post types you can use get_post_types to generate your list:

        function your_custom_post_types() {
    $args=array(
      '_builtin' => false
        );
        $cpt_types=get_post_types($args,'names');
        return $cpt_types;
    }
    
    
    
    
    
       <?php $post_types = your_custom_post_types();
                foreach ($post_types as $post_type) {
        echo '<li><a href="#">'.$post_type.'</a></li>';
    endforeach;