- This topic has 9 replies, 2 voices, and was last updated 3 years, 5 months ago by Tom.
-
AuthorPosts
-
November 9, 2020 at 4:11 pm #1524620qpaq
Hi,
I have a custom taxonomy called content-type and regular post categories.
On category archive pages, I’d like to display the custom taxonomy (content-type) name underneath the posts with a different icon as categories of each post are displayed.
November 10, 2020 at 9:42 am #1525707TomLead DeveloperLead DeveloperHi there,
We can add custom post meta items like this:
add_action( 'generate_post_meta_items', function( $item ) { if ( 'content-type' === $item ) { $types = get_the_term_list( get_the_ID(), 'content-type', '', ', ' ); if ( $types ) { printf( '<span class="content-type-links">%1$s%2$s</span>', apply_filters( 'generate_inside_post_meta_item_output', '', 'content-type' ), $types ); } } } ); add_filter( 'generate_footer_entry_meta_items', function( $items ) { return array( 'categories', 'content-type', 'tags', 'comments-link', 'post-navigation', ); } ); add_filter( 'generate_svg_icon_element', function( $output, $icon ) { if ( 'content-type' === $icon ) { $output = '<svg>..</svg>'; } return $output; } ); add_filter( 'generate_inside_post_meta_item_output', function( $output, $item ) { if ( 'content-type' === $item ) { generate_do_svg_icon( 'content-type' ); } }, 10, 2 );
Hope this helps!
November 10, 2020 at 3:27 pm #1526020qpaqThanks Tom, however it broke the site, downsizing the width of the page and gives the following error:
Recoverable fatal error: Object of class WP_Error could not be converted to string in /home/customer/www/sitename.com/public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()’d code on line 9
November 11, 2020 at 9:32 am #1527147TomLead DeveloperLead DeveloperStrange, I just tried it out on my localhost and it’s working fine.
Can you confirm that
content-type
is the correct taxonomy name?November 11, 2020 at 10:07 am #1527200qpaqMy mistake, sorry Tom.
content-type
should have beencontent_type
How do we insert an icon to echo ‘YOUR ICON HERE’;
and how do we get rid of the commas in between?
November 11, 2020 at 2:25 pm #1527480TomLead DeveloperLead DeveloperThat depends on the icon – is it inline SVG?
To remove the commas, replace this:
$types = get_the_term_list( get_the_ID(), 'content_type', '', ', ' );
With this:
$types = get_the_term_list( get_the_ID(), 'content_type' );
November 12, 2020 at 12:01 am #1527792qpaqThanks Tom, we don’t have commas in between now. The icon should be inline SVG.
November 12, 2020 at 10:20 am #1528762TomLead DeveloperLead DeveloperI’ve updated the code above to include an example of adding your own SVG to our SVG function: https://generatepress.com/forums/topic/post-meta-for-custom-taxonomy-archives-and-category-archive/#post-1525707
November 12, 2020 at 12:17 pm #1528886qpaqHi Tom,
the updated code generates an error.
The code snippet you are trying to save produced a fatal error on line 35:
syntax error, unexpected ”content-type ); ‘ (T_ENCAPSED_AND_WHITESPACE)November 12, 2020 at 7:20 pm #1529168TomLead DeveloperLead DeveloperSorry about that! Should be good to go now.
-
AuthorPosts
- You must be logged in to reply to this topic.