php – How to retrieve taxonomy Metabox fields in frontend

Question

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?

0
user3756781 1 month 2021-06-29T14:08:55-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse