Custom Post Type, Custom Taxonomy and Tags
-
I am having trouble working with a child theme of twentyseventeen and getting the custom taxonomy and tags to output on a single post page.
In child theme functions.php I have:
function faq_post_type() { $labels = array( 'name' => _x( 'Faqs', 'Post Type General Name', 'text_domain' ), 'singular_name' => _x( 'Faq', 'Post Type Singular Name', 'text_domain' ), 'menu_name' => __( 'Faqs', 'text_domain' ), 'parent_item_colon' => __( 'Parent Faq:', 'text_domain' ), 'all_items' => __( 'All Faqs', 'text_domain' ), 'view_item' => __( 'View Faq', 'text_domain' ), 'add_new_item' => __( 'Add New Faq', 'text_domain' ), 'add_new' => __( 'New Faq', 'text_domain' ), 'edit_item' => __( 'Edit Faq', 'text_domain' ), 'update_item' => __( 'Update Faq', 'text_domain' ), 'search_items' => __( 'Search Faqs', 'text_domain' ), 'not_found' => __( 'No Faqs Found', 'text_domain' ), 'not_found_in_trash' => __( 'No Faqs Found in Trash', 'text_domain' ), ); $args = array( 'label' => __( 'Faqs', 'text_domain' ), 'description' => __( 'Faqs', 'text_domain' ), 'labels' => $labels, 'hierarchical' => false, 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'show_in_nav_menus' => true, 'show_in_admin_bar' => true, 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'page-attributes', 'post-formats', ), 'taxonomies' => array('post_tag'), 'update_count_callback' => '_update_post_term_count', 'menu_position' => 5, 'menu_icon' => null, 'can_export' => true, 'has_archive' => true, 'exclude_from_search' => false, 'publicly_queryable' => true, 'capability_type' => 'post', 'rewrite' => array('slug' => 'faqs'), ); register_post_type( 'qa_faqs', $args ); // Hook into the 'init' action } add_action( 'init', 'faq_post_type', 0 ); // Register Custom Taxonomy function faq_taxonomy() { $labels = array( 'name' => _x( 'Faq Categories', 'Taxonomy General Name', 'text_domain' ), 'singular_name' => _x( 'Faq Category', 'Taxonomy Singular Name', 'text_domain' ), 'menu_name' => __( 'Faq Categories', 'text_domain' ), 'all_items' => __( 'All Faq Categories', 'text_domain' ), 'parent_item' => __( 'Parent Faq Category', 'text_domain' ), 'parent_item_colon' => __( 'Parent Faq Category:', 'text_domain' ), 'new_item_name' => __( 'New Faq Category Name', 'text_domain' ), 'add_new_item' => __( 'Add New Faq Category', 'text_domain' ), 'edit_item' => __( 'Edit Faq Category', 'text_domain' ), 'update_item' => __( 'Update Faq Category', 'text_domain' ), 'separate_items_with_commas' => __( 'Separate Faq Categories with commas', 'text_domain' ), 'search_items' => __( 'Search Faq Categories', 'text_domain' ), 'add_or_remove_items' => __( 'Add or Remove Faq Categories', 'text_domain' ), 'choose_from_most_used' => __( 'Choose from Most Used Faq Categories', 'text_domain' ), ); $args = array( 'labels' => $labels, 'hierarchical' => true, 'public' => true, 'show_ui' => true, 'show_admin_column' => true, 'show_in_nav_menus' => true, 'show_tagcloud' => false, 'rewrite' => array('slug' => 'faq-category'), ); register_taxonomy( 'faq_category', 'qa_faqs', $args ); } // Hook into the 'init' action add_action( 'init', 'faq_taxonomy', 0 ); add_filter('pre_get_posts', 'query_post_type'); function query_post_type($query) { if(is_category() || is_tag()) { $post_type = get_query_var('post_type'); if($post_type) $post_type = $post_type; else $post_type = array('nav_menu_item','post','faqs'); $query->set('post_type',$post_type); return $query; } }
When in the editor, I can see the all the faq-categories and tags are shown with each custom post. The post archive pages work fine. What I am having trouble with is getting the custom taxonomy and tags to to show on the single post page.
See these two examples.
Single Post Page using Category and Tag (default post behaviour).
Single Custom Post Type Page using Custom Taxonomy and Tag.
As you can see in the second link, we have nothing being output below the post for categories – which makes sense – I need to pull that in for my custom taxonomy. The question on that is what is the template part for that?? Using the What The File Plugin, I see the post is using single.php and it’s associated template parts, which I have looked at, and do not see where we include cats and tags anywhere…
As for not outputting the tags, I think I may need to adjust my functions.php file. I did some research and cannot find a solid working answer.
Hope above is clear enough. Thanks in advance!
- The topic ‘Custom Post Type, Custom Taxonomy and Tags’ is closed to new replies.