Archived topic
CPT tags are not showing. Why??
14 replies · Started by Susanne on July 31, 2021
Hi there,
I have built a site using the latest GP and GP premium plugin and a child theme.
I have a custom post Type "portfolio" and have created 3 categories for the CPT, and a filter menu, which display the 3 categories correctly.
However, I cannot get the tags to show either in the archive view or the single view. They show in the blog posts. They are correctly assigned to the portfolio posts ( I am using Portfolio post type plugin).
I have a single-portfolio and a content-portfolio.php in my child theme but no modifications so far.
Do I have to add something to the functions? I have made it work a couple of years ago in an older version of GP, but now things are different and I am lost.
The only relevant code in my functions is this and that works fine
add_filter('generate_blog_masonry','generate_blog_disable_blog_masonry');
function generate_blog_disable_blog_masonry()
{
// If we're on the posts page, disable masonry
if ( is_home() || is_page() || is_single() )
return 'false';
// Otherwise, enable it
return 'true';
}
What do I have to do? Thanks for your help
Hi there,
to enable the post meta on your CPT you can use this PHP Snippet:
add_filter( 'generate_entry_meta_post_types', function( $types ) {
$types[] = 'portfolio';
return $types;
} );
And then this snippet to tell it what you want to display in the Meta:
add_filter( 'generate_header_entry_meta_items', function( $items ) {
if ( 'portfolio' === get_post_type() ) {
$items = array(
'date',
);
}
return $items;
} );
More info on that filter here:
https://docs.generatepress.com/article/generate_header_entry_meta_items/
Or alternatively you can create your own Content Templates or Post Meta templates in the block editor:
https://docs.generatepress.com/article/block-element-content-template/
https://docs.generatepress.com/article/block-element-post-meta-template/
Hi David,
Thank you for your quick reply. I added these filters to my functions.php and got no results. I assume they go in the functions. Funny enough though, the date will show, which is the only one I don't want, but not the tags or categories.
Should I write these into the content-portfolio.php file instead of the functions?
On inspection of the archive page in Firefox, the
Any other ideas?
David's function above only includes date which is why only the date is showing.
Have you checked out the documentation he linked which shows you the rest of the items so you can tweak the code?
https://docs.generatepress.com/article/generate_header_entry_meta_items/
Hi Leo,
Thanks for your reply,
Yes I am aware of that and I modified the function to read
add_filter( 'generate_header_entry_meta_items', function() {
return array(
'date',
'tags',
'categories',
);
} );
and the date shows but not categories or tags
so something is not right.
I added
add_action( 'generate_post_meta_items', function( $item ) {
if ( 'portfolio-tags' === $item ) {
echo 'tags:';
}
} );
And the word 'tags' shows under the archive item, but not the tags themselves.They are portfolio tags, not regular tags
so I added
add_filter( 'generate_header_entry_meta_items', function() {
$items[] = 'portfolio-tags';
return $items;
} );
no tags are showing. But the nav displays the categories correctly, so they are identified, but not shown
If the CPT is using the default post_tag, the tag on generate_header_entry_meta_items should be enough. But this won't work if the portfolio-tags is a custom taxonomy.
Is the taxonomy of portfolio-tags a custom taxonomy? If yes, you'll have to tell generate_post_meta_items what portfolio-tags $item does so it does something when you call it on the generate_header_entry_meta_items filter.
Example: Say you have a taxonomy portfolio_tags, you'll have to do something like this.
add_filter( 'generate_entry_meta_post_types', function( $types ) {
$types[] = 'portfolio';
return $types;
} );
add_action( 'generate_post_meta_items', function( $item ) {
if ( 'portfolio-tags' === $item ) {
echo get_the_term_list( get_the_ID(), 'portfolio_tags', '', ', ' );
}
} );
add_filter( 'generate_header_entry_meta_items', function() {
return array(
'date',
'tags',
'categories',
'portfolio-tags',
);
} );
The first part of the snippet tells the theme to display meta on CPT portfolio.
The second part (important), tells the theme to pull terms from the custom taxonomy portfolio_tags.
The third part tells the theme to include the one we did on the second part on the render of the post meta. :D
Dear Elvin,
You are a genius, thank you soo much! I did have to modify the code a bit because my taxonomy was actually "portfolio_tag" not "portfolio_tags", but once I did that it actually worked. Thank you so much for taking the time to explain in detail how it works, that was so incredibly helpful. Thank you David an Leo also.
Your grateful customer
Susanne
Yeah those ones are always tricky.
No problem. Glad to be of any help. :D
One follow-up question: If I want a to assign a style to this tag, lets say .portfolio-tag, how would I do that? A filter?
Can you link me to an example page containing a post item w/ this custom taxonomy? So I can check which class selector is generated. :D
Hi Elvin,
I am developing locally, so can't link you to a post, but the tags are contained in the div class ="entry-meta", and above that in the div class="entry-header". thats all. So the tags themselves don't have an ID or a style. Hope that helps.
Ah in this case:
I see you seem familiar with the Google devtools (right-click + inspect).
You can check the <article> tag of the post. You on this tag, you'll see that on the class selectors, there are taxonomy classes.
You can see something like category-[your-category-slug] or tag-[your-tag-slug]. You'll likely see portfolio-[your-portfolio-slug] here as well. - https://share.getcloudapp.com/rRubqzD9
You can use this as a selector to style its ancestor elements.
Example: Changing the h2 within the article for portfolio "Dogs" to color red.
article.portfolio-dogs h2 { color: red; }
Thanks for that tip Elvin.Yes I see those selectors, however they are specific to that post. What I want to do is to create a filter or an action to assign a class to every portfolio tag within entry-meta.
I think I solved the problem by creating a hook in Elements (yeah Elements!) like this
<h5><?php echo get_the_term_list( $post->ID, 'portfolio_tag', 'Tags: ', ', ', '' ); ?></h5>
Now I have the tags showing with an h5 and I can style the h5 in the portfolio archive. But thank you so much for all of the thinking!
Ah yeah that can work as well. glad you got it sorted. :D