[Support request] display meta for custom taxonomies

Home Forums Support [Support request] display meta for custom taxonomies

Home Forums Support display meta for custom taxonomies

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #570004
    dale

    Howdy!

    I am using the Custom Post Type UI plugin to create custom taxonomies for several websites. For the website linked here, the custom taxonomy types are Format, Genre, and Pay Rate, and each has their own set of taxonomies that I add to organize the posts. For example, Genre has “adventure”, “sci fi”, “romance”, and so forth as possible taxonomies.

    How can I display the custom taxonomy type and “tags” in post meta? I definitely would like to display it on individual posts in the meta section (along with tags and categories). It’s less important to display them with post experts on the front page of the blog, but it would be nice to have that option too.

    (And if you recommend a different plugin or code for custom taxonomies, let me know. I’ve only installed Custom Post Type UI on one site so far.)

    Thanks!

    #570222
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    So right now you’re displaying categories and tags, and you want to include your custom taxonomy terms after the tags?

    Let me know ๐Ÿ™‚

    #570935
    dale

    No, I want to display them as “format: blah blah blah” “genre: yadda yadda” and so forth. Their own thing, like tags and categories meta are their own things.

    Did that make sense?

    #571097
    Tom
    Lead Developer
    Lead Developer

    Somewhat complex, but you could try something like this:

    add_action( 'generate_after_entry_content', 'tu_add_custom_tax_terms', 50 );
    function tu_add_custom_tax_terms() {
    	$taxonomies = array( 
    	  'post_tag' => 'Tags', 
    	  'category' => 'Categories',
    	);
      
      	echo '<footer class="entry-meta">';
    
      		foreach ( $taxonomies as $id => $name ) {
                            $term_list = get_the_term_list( get_the_ID(), $id, '', ', ' );
                            if ( $term_list ) {
    	  		    echo '<div class="taxonomy">';
    				echo $name . ': ' . get_the_term_list( get_the_ID(), $id, '', ', ' );
    	  		    echo '</div>';
                            }
    		}
      
      	echo '</footer>';
    }
    #571284
    dale

    Thanks! For some reason, commas aren’t appearing between each term. Do I need to add something else? This is the code I used:

    add_action( 'generate_after_entry_content', 'tu_add_custom_tax_terms', 50 );
    function tu_add_custom_tax_terms() {
    	$taxonomies = array( 
    	  'post_tag' => 'Tags', 
    	  'category' => 'Categories',
    	  'genres' => 'Genres',
    	  	  'format' => 'Format',
    	  	  'payment' => 'Payment',
    
    	);
      
      	echo '<footer class="entry-meta">';
    
      		foreach ( $taxonomies as $id => $name ) {
    	  		echo '<div class="taxonomy">';
    				echo $name . ': ' . get_the_term_list( get_the_ID(), $id, '', '', '' );
    	  		echo '</div>';
    		}
      
      	echo '</footer>';
    }

    You can see an example of how it generated at the bottom of https://www.dalecameronlowry.com/submission-call-cosmic-roots-eldritch-shores-june/

    Also, to remove/hide the taxonomy (for example tags) when no terms are added, would I add an if/then line under

    echo $name . ': ' . get_the_term_list( get_the_ID(), $id, '', '', '' );

    Thanks so much!

    #571290
    dale

    P.S. to add this via GP hooks, would I do it as:

    <?php {
    ?>
    add_action(‘tu_add_custom_tax_terms’, 50 );
    function tu_add_custom_tax_terms() {
    $taxonomies = array(
    ‘post_tag’ => ‘Tags’,
    ‘category’ => ‘Categories’,
    ‘genres’ => ‘Genres’,
    ‘format’ => ‘Format’,
    ‘payment’ => ‘Payment’,

    );

    echo ‘<footer class=”entry-meta”>’;

    foreach ( $taxonomies as $id => $name ) {
    echo ‘

    ‘;
    echo $name . ‘: ‘ . get_the_term_list( get_the_ID(), $id, ”, ”, ” );
    echo ‘

    ‘;
    }

    echo ‘</footer>’;
    }
    <?php }
    ?>

    #571580
    Tom
    Lead Developer
    Lead Developer

    Looks like you removed the comma from the code I provided.

    This is what it should be: get_the_term_list( get_the_ID(), $id, '', ', ' );

    This is what you have: get_the_term_list( get_the_ID(), $id, '', '', '' );

    I just edited the code above so they should only display if terms exist: https://generatepress.com/forums/topic/display-meta-for-custom-taxonomies/#post-571097

    That code looks good for GP Hooks ๐Ÿ™‚

    #571661
    dale

    Thank you! It works great when added to functions php.

    For some reason I couldn’t get it to work in GP hooks. I used:

    <?php
    if ( in_category( 'for-writers' ) && is_single() )
    { ?>
    
    add_action( 'tu_add_custom_tax_terms', 50 );
    function tu_add_custom_tax_terms() {
    	$taxonomies = array( 
    	  'post_tag' => 'Tags', 
    	  'category' => 'Categories',
    	);
      
      	echo '<footer class="entry-meta">';
    
      		foreach ( $taxonomies as $id => $name ) {
                            $term_list = get_the_term_list( get_the_ID(), $id, '', ', ' );
                            if ( $term_list ) {
    	  		    echo '<div class="taxonomy">';
    				echo $name . ': ' . get_the_term_list( get_the_ID(), $id, '', ', ' );
    	  		    echo '</div>';
                            }
    		}
      
      	echo '</footer>';
    }
    <?php }
    ?>

    Did I make an error?

    #571990
    Tom
    Lead Developer
    Lead Developer

    It would need to look like this in GP Hooks:

    <?php if ( in_category( 'for-writers' ) && is_single() ) : ?>
        $taxonomies = array( 
            'post_tag' => 'Tags', 
            'category' => 'Categories',
        );
    
        echo '<footer class="entry-meta">';
    
            foreach ( $taxonomies as $id => $name ) {
                $term_list = get_the_term_list( get_the_ID(), $id, '', ', ' );
                if ( $term_list ) {
                    echo '<div class="taxonomy">';
                        echo $name . ': ' . get_the_term_list( get_the_ID(), $id, '', ', ' );
                    echo '</div>';
                }
            }
      
        echo '</footer>';
    <?php endif; ?>
Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.