• 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!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter Pioneer Web Design

    (@swansonphotos)

    OK, I found the function that does the entry footer in template-parts/post/content.php

    
    <?php if ( is_single() ) : ?>
    	<?php twentyseventeen_entry_footer(); ?>
    <?php endif; ?>
    

    Now, where is the function twentyseventeen_entry_footer defined and how to resolve above?

    Also, now that the twentyseventeen theme is active, perhaps this thread should be moved there?

    Thread Starter Pioneer Web Design

    (@swansonphotos)

    OK – now I found that in inc/template-tags.php

    
    if ( ! function_exists( 'twentyseventeen_entry_footer' ) ) :
    /**
     * Prints HTML with meta information for the categories, tags and comments.
     */
    function twentyseventeen_entry_footer() {
    
    	/* translators: used between list items, there is a space after the comma */
    	$separate_meta = __( ', ', 'twentyseventeen' );
    
    	// Get Categories for posts.
    	$categories_list = get_the_category_list( $separate_meta );
    
    	// Get Tags for posts.
    	$tags_list = get_the_tag_list( '', $separate_meta );
    
    	// We don't want to output .entry-footer if it will be empty, so make sure its not.
    	if ( ( ( twentyseventeen_categorized_blog() && $categories_list ) || $tags_list ) || get_edit_post_link() ) {
    
    		echo '<footer class="entry-footer">';
    
    			if ( 'post' === get_post_type() ) {
    				if ( ( $categories_list && twentyseventeen_categorized_blog() ) || $tags_list ) {
    					echo '<span class="cat-tags-links">';
    
    						// Make sure there's more than one category before displaying.
    						if ( $categories_list && twentyseventeen_categorized_blog() ) {
    							echo '<span class="cat-links">' . twentyseventeen_get_svg( array( 'icon' => 'folder-open' ) ) . '<span class="screen-reader-text">' . __( 'Categories', 'twentyseventeen' ) . '</span>' . $categories_list . '</span>';
    						}
    
    						if ( $tags_list ) {
    							echo '<span class="tags-links">' . twentyseventeen_get_svg( array( 'icon' => 'hashtag' ) ) . '<span class="screen-reader-text">' . __( 'Tags', 'twentyseventeen' ) . '</span>' . $tags_list . '</span>';
    						}
    
    					echo '</span>';
    				}
    			}
    
    			twentyseventeen_edit_link();
    
    		echo '</footer> <!-- .entry-footer -->';
    	}
    }
    endif;
    

    So, looks like we need to fix these strings:

    
    // Get Categories for posts.
    $categories_list = get_the_category_list( $separate_meta );
    
    // Get Tags for posts.
    $tags_list = get_the_tag_list( '', $separate_meta );
    

    adjusting for our new custom taxonomy, we need more info on that….and, again, why are the tags not being output?

    Hi,

    It seems like the function get_the_category_list() and get_the_tag_list() is for default post type which is “post”, in your case, its “qa_faqs”, so i believe this function https://codex.wordpress.org/Function_Reference/get_the_term_list needs to be used,
    you can try this below code instead

    
    global $post;
    echo get_the_term_list( $post->ID, 'post_tag', '<ul class="styles"><li>', ',</li><li>', '</li></ul>' );
    

    Let me know if it works for you.

    Thread Starter Pioneer Web Design

    (@swansonphotos)

    It’s more than that I believe, the function twentyseventeen_entry_footer exists in the parent theme, so I will need to create a new function, based on it, and also get my terms. I will post what worked when I am get it working. If I just nix that function, the normal posts will also need fixing.

    I have so far, in child theme, created the inc folder, copied the file template-tags.php to it, edited the function to test, all to no avail. Apparently that file is not working like other template files?

    it wont work, because overriding of files are also having some limitations. You can just copy single.php of your parent theme and put it in child theme, rename it to single-qa_faqs.php, so that its just for specific “qa_faqs” post type and in that way you can customize whatever you want, change the function name and use it on your own way. It needs bit of digging in theme files so if you want you can hire someone for that.

    Thread Starter Pioneer Web Design

    (@swansonphotos)

    I am aware of how to setup a child theme, have done many, most with custom post types – without having the issue of having to deal with the entry-footer function – just a different approach there, which varies from previous default themes.

    And, I shall, as usual, hire myself 🙂

    Thread Starter Pioneer Web Design

    (@swansonphotos)

    I have an even better question…

    Even though many good hosts are now setting PHP7 as the default, which does appreciably increase site performance, we still see that any time we can stay within PHP in any WordPress (or PHP, WP is PHP) template file, that we should…it’s even shown in the Codex as what I can only consider to be a Best Practice:

    This code:

    
    <?php if ( is_single() ) : ?>
    	<?php twentyseventeen_entry_footer(); ?>
    <?php endif; ?>
    

    Needs only simply be:

    
    <?php if ( is_single() ) :
    	//some function here including the one above
     endif;
    ?>
    

    Why do we need to enter PHP 3 times for this in 2017 (a time, not a theme)?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Custom Post Type, Custom Taxonomy and Tags’ is closed to new replies.