Community Forums › Forums › Archived Forums › General Discussion › Different content types on shared category archive page
- This topic has 1 reply, 1 voice, and was last updated 10 years ago by
nkleekamp.
-
AuthorPosts
-
January 20, 2015 at 4:03 pm #138133
nkleekamp
MemberHello! I created a custom content type called "Publication" based on the portfolio content type code in the Executive Pro child theme, with some additions.
//* Create Publication custom post type add_action( 'init', 'executive_publication_post_type' ); function executive_publication_post_type() { register_post_type( 'publication', array( 'labels' => array( 'name' => __( 'Publications', 'executive' ), 'singular_name' => __( 'Publication', 'executive' ), 'add_new' => __( 'Add New', 'publication'), 'add_new_item' => __( 'Add New Publication' ), 'edit_item' => __( 'Edit Publication' ), 'new_item' => __( 'New Publication' ), 'all_items' => __( 'All Publications' ), 'view_items' => __( 'View Publication' ), 'search_items' => __( 'Search Publications' ), 'not_found' => __( 'No publications found' ), 'not_found_in_trash' => __( 'No publications found in the Trash' ), 'parent_item_colon' => '', 'menu_name' => 'Publications' ), 'has_archive' => true, 'hierarchical' => true, 'menu_icon' => get_stylesheet_directory_uri() . '/lib/icons/publications.png', 'public' => true, 'menu_position' => 3, 'rewrite' => array( 'slug' => 'publication', 'with_front' => false ), 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'custom-fields', 'revisions', 'genesis-seo', 'genesis-cpt-archives-settings' ), 'taxonomies' => array( 'publication-type', 'category', 'post_tag' ), ) ); }
I've also created the "Type" custom taxonomy in the publication custom content type.
//* Create Publication Type custom taxonomy add_action( 'init', 'executive_publication_type_taxonomy' ); function executive_publication_type_taxonomy() { register_taxonomy( 'publication-type', 'publication', array( 'labels' => array( 'name' => _x( 'Types', 'taxonomy general name', 'executive' ), 'add_new_item' => __( 'Add New Publication Type', 'executive' ), 'new_item_name' => __( 'New Publication Type', 'executive' ), ), 'exclude_from_search' => true, 'has_archive' => true, 'hierarchical' => true, 'rewrite' => array( 'slug' => 'publications', 'with_front' => false ), 'show_ui' => true, 'show_tagcloud' => false, ) ); }
When defining the "Publication" custom content type, I included
category
andpost_tag
in the taxonomies declaration, thinking that this would make post categories and tags available to my "Publication" custom content type. However, when I view a category, I only see posts with that category; my custom content type "publications" do not appear in the category archive page. How can I get all content types with the shared category to appear in the category archive page?January 20, 2015 at 10:10 pm #138158nkleekamp
MemberI found the answer on StackExchange and expanded it with a little more info from the Codex.
For anyone else that might need it, here was my solution.
//* Include Custom Post Types in author, category, and tag archive page function __set_cpt_for_archives( &$query ) { $post_types = array( 'post', 'portfolio', 'publication' ); $archive_types = array( 'is_author', 'is_tag', 'is_category' ); foreach ( $archive_types as $key => $val ) { if ( $query->$val ) { $query->set( 'post_type', $post_types ); remove_action( 'pre_get_posts', '__set_cpt_for_archives' ); } } } add_action( 'pre_get_posts', '__set_cpt_for_archives' );
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.