Archived topic
Post Meta for Custom Taxonomy Archives and Category Archive
9 replies · Started by qpaq on November 9, 2020
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.
Hi 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!
Thanks 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
Strange, I just tried it out on my localhost and it's working fine.
Can you confirm that content-type is the correct taxonomy name?
My mistake, sorry Tom. content-type should have been content_type
How do we insert an icon to echo 'YOUR ICON HERE';
and how do we get rid of the commas in between?
That 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' );
Thanks Tom, we don't have commas in between now. The icon should be inline SVG.
I'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
Hi 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)
Sorry about that! Should be good to go now.