How to retrieve taxonomy Metabox fields in frontend

I have a custom taxonomy with custom fields which are being populated with CMB2. I can’t seem to figure out why I am unable to populate this information on the frontend.

Below is the code that is generating the custom fields on my custom taxonomy called Placement.

     add_action( 'cmb2_admin_init', 'placement_register_taxonomy_metabox' ); 
 function placement_register_taxonomy_metabox() { 
    $prefix = 'placement_'; 
    $cmb_term = new_cmb2_box( array( 
        'id'               => $prefix . 'placement', 
        'title'            => esc_html__( 'Title Handler', 'veruscref-theme' ), // Doesn't output for term boxes 
        'object_types'     => array( 'term' ), // Tells CMB2 to use term_meta vs post_meta 
        'taxonomies'       => array( 'placement' ), // Tells CMB2 which taxonomies should have these fields 
        // 'new_term_section' => true, // Will display in the "Add New Category" section 
    ) ); 
 $cmb_term->add_field( array( 
        'name'     => esc_html__( 'Loan Program Title', 'veruscref-theme' ), 
        'desc'     => esc_html__( 'Will be displayed on transaction page', 'veruscref-theme' ), 
        'id'       => $prefix . 'tax_header', 
        'type'     => 'title', 
        'on_front' => false, 
    ) ); 

    $cmb_term->add_field( array( 
        'name' => esc_html__( 'Title', 'veruscref-theme' ), 
        'id'   => $prefix . 'tax_title', 
        'type' => 'text', 
    ) ); 
     $cmb_term->add_field( array( 
        'name' => esc_html__( 'Small Title', 'veruscref-theme' ), 
        'id'   => $prefix . 'tax_small_title', 
        'type' => 'text', 
    ) );

Below is my frontend code

<?php
$terms = get_terms(
array(
    'taxonomy'   => 'placement',
    'hide_empty' => 1,
    'exclude' => array(4,23),
)
);
if ( ! empty( $terms ) && is_array( $terms ) ) { foreach ( $terms as $term ) { ?>       
<?php                        
$title = get_term_meta( get_queried_object_id(), 'placement_tax_title', true );
echo $title;                                             
?>  
<?php } } ?>

What am I doing wrong with my code?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You need to use $term->term_id not get_queried_object_id() in get_term_meta


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x