[Support request] Post Meta for Custom Taxonomy Archives and Category Archive

Home Forums Support [Support request] Post Meta for Custom Taxonomy Archives and Category Archive

Home Forums Support Post Meta for Custom Taxonomy Archives and Category Archive

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1524620
    qpaq

    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.

    #1525707
    Tom
    Lead Developer
    Lead Developer

    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!

    #1526020
    qpaq

    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

    #1527147
    Tom
    Lead Developer
    Lead Developer

    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?

    #1527200
    qpaq

    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?

    #1527480
    Tom
    Lead Developer
    Lead Developer

    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' );

    #1527792
    qpaq

    Thanks Tom, we don’t have commas in between now. The icon should be inline SVG.

    #1528762
    Tom
    Lead Developer
    Lead Developer

    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

    #1528886
    qpaq

    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)

    #1529168
    Tom
    Lead Developer
    Lead Developer

    Sorry about that! Should be good to go now.

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