[Resolved] CPT tags are not showing. Why??

Home Forums Support [Resolved] CPT tags are not showing. Why??

Home Forums Support CPT tags are not showing. Why??

Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #1878819
    Susanne

    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

    #1879140
    David
    Staff
    Customer Support

    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/

    #1879702
    Susanne

    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

    shows up but has no content. I am sure I did something wrong, but I cannot figure out what it is. I am developing on MAMP.
    Any other ideas?
    #1879709
    Leo
    Staff
    Customer Support

    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/

    #1879734
    Susanne

    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.

    #1879750
    Susanne

    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

    #1879998
    Elvin
    Staff
    Customer Support

    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. πŸ˜€

    #1881332
    Susanne

    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

    #1881371
    Elvin
    Staff
    Customer Support

    Yeah those ones are always tricky.

    No problem. Glad to be of any help. πŸ˜€

    #1881381
    Susanne

    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?

    #1881512
    Elvin
    Staff
    Customer Support

    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. πŸ˜€

    #1882937
    Susanne

    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.

    #1882955
    Elvin
    Staff
    Customer Support

    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; }

    #1883320
    Susanne

    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!

    #1884328
    Elvin
    Staff
    Customer Support

    Ah yeah that can work as well. glad you got it sorted. πŸ˜€

Viewing 15 posts - 1 through 15 (of 15 total)
  • You must be logged in to reply to this topic.