In a WordPress plugin I have custom post types for different types of publication: books, chapters, papers, presentations, reports. I want one single archive of all of these publications.
I know that the theme template hierarchy allows templates with the pattern archive-$posttype.php, so
I tried setting the slug for all the custom post types to ‘presentations’. WordPress doesn’t like that. So what I did was set the slug for one of the publication custom post types to ‘presentations’, that gives me a /presentations/ archive for that custom post type(1). I then edited the archive.php
file to use a different template parts for custom post types(2):
<?php $cpargs = array('_builtin' => False, 'exclude_from_search' => False); $custom_post_types = get_post_types( $cpargs, 'names', 'and' ); if ( is_post_type_archive( $custom_post_types ) ) { get_template_part( 'archive-publication' ); } else { get_template_part( 'archive-default' ); } ?>
See anything wrong with this approach? Any comments on how better to do this would be welcome.
Notes:
- 1 could edit the .htaccess file to redirect the /books/, /chapters/ …etc archives to /publications/, which would be neater in some ways but would make setting up the theme a bit of a faff.
- Yes, the code gives all the custom post types with an archive the same archive. That’s fixable if you make the array of post types for which you want a shared archive manually.
The post Shared WordPress archive for different post types appeared first on Sharing and learning.