Last active
September 29, 2019 01:50
-
-
Save malinky/4899384e1de5e4ab3338 to your computer and use it in GitHub Desktop.
Wordpress Custom Post Type Landing Pages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Wordpress Custom Post Type Landing Pages | |
* In all examples $rewrite and $args are simplified to the discussed settings. | |
*/ | |
/** | |
* 1. Custom Post Type has archives and the slug is rewritten. | |
* In this case a visit to /post-type-name will use archive-$posttype.php, $archive.php, index.php. | |
* Pagination will work by default assuming $rewrite[ 'pages' ] is also true (default). | |
* It's not possible to create a landing page called /post-type-name as the archive page controls the template hierarchy. | |
* If a specific landing page is required the landing page will need a different slug or the $rewrite slug needs to be different. | |
* If creating a landing page ensure a custom wp_query object is used and is still in use when calling pagination links or these | |
* won't work. | |
*/ | |
$rewrite = array( | |
'slug' => 'post-type-name' | |
); | |
$args = array( | |
has_archive => true, | |
'rewrite' => $rewrite, | |
) | |
register_post_type ( 'post_type_name', $args ) | |
/** | |
* 2. Custom Post Type has archives and the slug is not rewritten. | |
* In this case a visit to /post_type_name will use archive-$posttype.php, $arhvice.php, index.php. | |
* Pagination will work by default assuming $rewrite[ 'pages' ] is also true (default). | |
* A landing page can be created with the slug /post-type-name... | |
* A template can be applied using a custom loop to replace wp_query and ensure pagination works. | |
*/ | |
$args = array( | |
has_archive => true | |
) | |
register_post_type ( 'post_type_name', $args ) | |
/** | |
* 3. Custom Post Type doesn't have archive. | |
* The slug rewrite is irrelevant as the custom post type archives are not available. | |
* Therefore a visit to /post-type-name will return a 404 or a landing page if one is created. | |
* A landing page can be created and a template applied using a custom loop to replace wp_query and ensure pagination works. | |
* See notes above regarding pagination of this. | |
*/ | |
$rewrite = array( | |
'slug' => 'post-type-name' | |
); | |
$args = array( | |
has_archive => false, | |
'rewrite' => $rewrite, | |
) | |
register_post_type ( 'post_type_name', $args ) | |
/** | |
* Useful reading... | |
* http://callmenick.com/2014/02/21/custom-wordpress-loop-with-pagination/ | |
* http://code.tutsplus.com/articles/custom-post-type-pagination-chaining-method--wp-21444 | |
* http://css-tricks.com/snippets/wordpress/paginate-custom-post-types/ | |
* http://codex.wordpress.org/Function_Reference/register_post_type | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment