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')
));
}
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
: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()
:So, for example, to return public, custom post types by name:
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()
:So we just step trough our array of post type names, and retrieve the permalink for each:
Then you can use that array of permalinks (URLs) to create your list markup:
Putting it all together
Your
template-cpt-list.php
should now look like so: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.
To list out all of the custom post types you can use get_post_types to generate your list: