• Resolved Layson

    (@glennlaysonjr)


    I have added the following code:

    add_action( 'cmb2_admin_init', 'series_taxonomy_metabox' );
    /**
     * Hook in and add a metabox to add fields to taxonomy terms
     */
    function series_taxonomy_metabox() {
    	$prefix = 'dc_tax';
    
    	/**
    	 * Metabox to add fields to categories and tags
    	 */
    	$cmb_term = new_cmb2_box( array(
    		'id'               => $prefix . 'series_tax',
    		'title'            => esc_html__( 'Series Metabox', 'cmb2' ), // Doesn't output for term boxes
    		'object_types'     => array( 'term' ), // Tells CMB2 to use term_meta vs post_meta
    		'taxonomies'       => array( 'series_hub' ), // 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__( 'Term Image', 'cmb2' ),
    		'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
    		'id'   => $prefix . 'avatar',
    		'type' => 'file',
    	) );
    
    }

    This creates a file field and saves to database.

    My problem I am having is I can’t display my data on my category template page.

    I am using the following code as a test just to display the url. Then I’m going to add all if’s that i need and img tag.

    <?php
     $avatar = get_term_meta( get_the_ID(), 'dc_tax_avatar', true );
       if(empty($avatar)){
       echo 'empty';
     } else {
       echo '<span style="color:#fff;">'. $avatar .'</span>';
     }
    ?>

    Any ideas on why its not working?

    • This topic was modified 7 years, 3 months ago by Layson.
    • This topic was modified 7 years, 3 months ago by Layson.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Only thing that stands out is that you’re setting this as term meta, and you’re passing a post ID to get_term_meta() instead of the associated term ID.

    Thread Starter Layson

    (@glennlaysonjr)

    I have tried a couple of different things. This echos out the id but code still not working. Any chance you could give me any more detailed example.

    <?php
    $term_id = get_queried_object()->term_id;
    $avatar = get_term_meta( $term_id, 'dc_tax_avatar', true );
    if(empty($avatar)){
    echo 'still doesnt work';
    } else {
    echo '<span style="color:#fff;">'. $avatar .'</span>';
    }
    ?>

    I have also tried this:

    <?php get_term_meta(get_queried_object()->term_id, 'dc_tax_avatar', true ); ?>
    Thanks for your help on this

    • This reply was modified 7 years, 3 months ago by Layson.
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Based on the $prefix variable and the id value, the term meta key is dc_taxavatar. Notice you don’t have the trailing “_” on the prefix that would separate the words

    Thread Starter Layson

    (@glennlaysonjr)

    You are correct. Sorry I missed that.

    I updated my prefix to dc_tax_ and it works great.

    Thanks for seeing that!

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Welcome. ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Category Metabox Echo Not Displaying’ is closed to new replies.