Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

I want to display this in a list.

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')
    ));
}
share|improve this question
    
Please post your register_post_type() code. Are you referring to a single custom post type (CPT) or to multiple CPTs, "campsites", "evennts", and "activities"? –  Chip Bennett Nov 21 '12 at 14:43
    
I am referring to multiple CPTs 'campsites, events and activities' –  mandy Nov 21 '12 at 14:51

2 Answers 2

up vote 3 down vote accepted

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();

?>
share|improve this answer
    
wow all working :) thank you sooooo much :) –  mandy Nov 23 '12 at 13:26

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;
share|improve this answer
    
I want to just display the different custom post types ie campsites, events and activities. I got a template for each CPT already which displays the post from each CPT. Is there a way to list all the registered CPTs? Then when the user click on campsites, it will take them to the page which list all the post in the campsites CPT? –  mandy Nov 21 '12 at 15:24
    
You will need to update the href="#" with something like get_site_url()/path-to-cpt-pages –  Rachel Baker Nov 21 '12 at 15:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.