Last active
April 27, 2022 22:24
-
-
Save malinky/0b747d74b1abb8a72618 to your computer and use it in GitHub Desktop.
Wordpress Custom Post Type - Archive and Landing Page Variations
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
| /* ------------------------------------------------------------------------ * | |
| * Custom post type and taxonomy with archives. (Preferable URLS) | |
| * ------------------------------------------------------------------------ */ | |
| /** | |
| * No landing page added to admin. | |
| * Use archive template hierarchy to create a landing page if requried. | |
| * No custom wp_query is requried for pagination, it just works. | |
| * Obviously need to add post_type_link filter for the URLS to include the taxonomy. | |
| * As there is no landing page harder to add to a menu. https://wordpress.org/plugins/post-type-archive-links/ | |
| * The posttype-slug in the URL is the first part of $rewrite slug. | |
| * Thanks to http://wordpress.stackexchange.com/a/22490 | |
| * | |
| * URLS | |
| * | |
| * /posttype-slug /records | |
| * archive-$posttype.php, $archive.php and so on) | |
| * | |
| * /posttype-slug/term-slug /records/breaks | |
| * (taxonomy-$taxonomy-$term.php, taxonomy-$taxonomy.php, taxonomy.php and so on) | |
| * | |
| * /posttype-slug/term-slug/post /records/breaks/plastic-dreams | |
| * (single-$posttype.php, single.php and so on) | |
| * | |
| */ | |
| function malinky_cpt() | |
| { | |
| $singular = 'Genre'; | |
| $plural = 'Genres'; | |
| $rewrite = array( | |
| 'slug' => 'records' | |
| ); | |
| $args = array( | |
| 'label' => $plural, | |
| 'labels' => array( | |
| 'name' => $plural, | |
| 'singular_name' => $singular, | |
| ), | |
| 'show_ui' => true, | |
| 'hierarchical' => true, | |
| 'query_var' => 'records-genre', /* ?records-genre=term */ | |
| 'rewrite' => $rewrite | |
| ); | |
| register_taxonomy( 'malinky-records-genre', 'malinky-records-type', $args ); | |
| $singular = 'Record'; | |
| $plural = 'Records'; | |
| $rewrite = array( | |
| 'slug' => 'records/%malinky-records-genre%', /* %taxonomy-name% */ | |
| 'with_front' => false | |
| ); | |
| $args = array( | |
| 'label' => $plural, | |
| 'labels' => array( | |
| 'name' => $plural, | |
| 'singular_name' => $singular, | |
| ), | |
| 'public' => true, | |
| 'supports' => array( 'title', 'custom-fields', 'author', 'editor' ), | |
| 'taxonomies' => array( 'malinky-records-genre' ), | |
| 'has_archive' => 'records', /* url for landing page of archive, same as first part of each $rewrite slug */ | |
| 'rewrite' => $rewrite, | |
| 'query_var' => 'records-type' /* ?records-type=permalink */ | |
| ); | |
| register_post_type( 'malinky-records-type', $args ); | |
| } | |
| add_action( 'init', 'malinky_cpt'); | |
| function malinky_post_type_link( $link, $post ) | |
| { | |
| if ( $post->post_type != 'malinky-records-type' ) return $link; | |
| if ( $terms = get_the_terms( $post->ID, 'malinky-records-genre' ) ) { | |
| $link = str_replace( '%malinky-records-genre%', array_pop( $terms )->slug, $link ); | |
| } | |
| return $link; | |
| } | |
| add_filter( 'post_type_link', 'malinky_post_type_link', 10, 4 ); | |
| /* ------------------------------------------------------------------------ * | |
| * Custom post type and taxonomy with archives. (Standard URLS) | |
| * ------------------------------------------------------------------------ */ | |
| /** | |
| * No landing page added to admin. | |
| * Use archive template hierarchy to create a landing page if requried. | |
| * No custom wp_query is requried for pagination, it just works. | |
| * As there is no landing page harder to add to a menu. https://wordpress.org/plugins/post-type-archive-links/ | |
| * Thanks to http://wordpress.stackexchange.com/a/22490 | |
| * | |
| * URLS | |
| * | |
| * /posttype-slug /records | |
| * archive-$posttype.php, $archive.php and so on) | |
| * | |
| * /taxonomy-slug/term-slug /records-genre/breaks | |
| * (taxonomy-$taxonomy-$term.php, taxonomy-$taxonomy.php, taxonomy.php and so on) | |
| * | |
| * /posttype-slug/post /records/plastic-dreams | |
| * (single-$posttype.php, single.php and so on) | |
| * | |
| */ | |
| function malinky_cpt() | |
| { | |
| $singular = 'Genre'; | |
| $plural = 'Genres'; | |
| $rewrite = array( | |
| 'slug' => 'records-genre' | |
| ); | |
| $args = array( | |
| 'label' => $plural, | |
| 'labels' => array( | |
| 'name' => $plural, | |
| 'singular_name' => $singular, | |
| ), | |
| 'show_ui' => true, | |
| 'hierarchical' => true, | |
| 'query_var' => 'records-genre', /* ?records-genre=term */ | |
| 'rewrite' => $rewrite | |
| ); | |
| register_taxonomy( 'malinky-records-genre', 'malinky-records-type', $args ); | |
| $singular = 'Record'; | |
| $plural = 'Records'; | |
| $rewrite = array( | |
| 'slug' => 'records', | |
| 'with_front' => false | |
| ); | |
| $args = array( | |
| 'label' => $plural, | |
| 'labels' => array( | |
| 'name' => $plural, | |
| 'singular_name' => $singular, | |
| ), | |
| 'public' => true, | |
| 'supports' => array( 'title', 'custom-fields', 'author', 'editor' ), | |
| 'taxonomies' => array( 'malinky-records-genre' ), | |
| 'has_archive' => 'records', /* url for landing page of archive, same as $rewrite slug */ | |
| 'rewrite' => $rewrite, | |
| 'query_var' => 'records-type' /* ?records-type=permalink */ | |
| ); | |
| register_post_type( 'malinky-records-type', $args ); | |
| } | |
| add_action( 'init', 'malinky_cpt'); | |
| /* ------------------------------------------------------------------------ * | |
| * Custom post type and taxonomy with NO archives. (Standard URLS) | |
| * ------------------------------------------------------------------------ */ | |
| /** | |
| * Landing page is added to admin with slug records. | |
| * Create page template in theme and select in the admin screen for the page. See below page-records.php | |
| * Custom wp_query is requried for pagination. | |
| * Note singular in postype $rewrite slug too avoid conflict with admin page on paging. | |
| * | |
| * URLS | |
| * | |
| * /admin-page-slug /records | |
| * $custom.php, page-$slug.php and so on) | |
| * | |
| * /taxonomy-slug/term-slug /records-genre/breaks | |
| * (taxonomy-$taxonomy-$term.php, taxonomy-$taxonomy.php, taxonomy.php and so on) | |
| * | |
| * /posttype-slug/post /record/plastic-dreams | |
| * (single-$posttype.php, single.php and so on) | |
| * | |
| */ | |
| function malinky_cpt() | |
| { | |
| $singular = 'Genre'; | |
| $plural = 'Genres'; | |
| $rewrite = array( | |
| 'slug' => 'records-genre' | |
| ); | |
| $args = array( | |
| 'label' => $plural, | |
| 'labels' => array( | |
| 'name' => $plural, | |
| 'singular_name' => $singular, | |
| ), | |
| 'show_ui' => true, | |
| 'hierarchical' => true, | |
| 'query_var' => 'records-genre', /* ?records-genre=term */ | |
| 'rewrite' => $rewrite | |
| ); | |
| register_taxonomy( 'malinky-records-genre', 'malinky-records-type', $args ); | |
| $singular = 'Record'; | |
| $plural = 'Records'; | |
| $rewrite = array( | |
| 'slug' => 'record', /* Singular as admin page has slug records */ | |
| 'with_front' => false | |
| ); | |
| $args = array( | |
| 'label' => $plural, | |
| 'labels' => array( | |
| 'name' => $plural, | |
| 'singular_name' => $singular, | |
| ), | |
| 'public' => true, | |
| 'supports' => array( 'title', 'custom-fields', 'author', 'editor' ), | |
| 'taxonomies' => array( 'malinky-records-genre' ), | |
| 'rewrite' => $rewrite, | |
| 'query_var' => 'records-type' /* ?records-type=permalink */ | |
| ); | |
| register_post_type( 'malinky-records-type', $args ); | |
| } | |
| add_action( 'init', 'malinky_cpt'); | |
| /** | |
| * Template Name: Page Records | |
| * File Name: page-records.php | |
| */ | |
| if ( get_query_var('page') ) $paged = get_query_var('page'); | |
| $temp = $wp_query; | |
| $wp_query = malinky_groundcare_machinery_wp_query( $paged ); | |
| if ( get_query_var('paged') ) $paged = get_query_var('paged'); | |
| if ( get_query_var('page') ) $paged = get_query_var('page'); | |
| $temp = $wp_query; | |
| $args = array( | |
| 'post_type' => 'malinky-records-type', | |
| 'posts_per_page' => 1, | |
| 'paged' => $paged | |
| ); | |
| $wp_query = new WP_Query( $args ); | |
| while ( $wp_query->have_posts() ) : $wp_query->the_post(); | |
| the_title(); | |
| previous_posts_link( 'Prev' ); | |
| next_posts_link( 'Next' ); | |
| endwhile; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment