0

I'm using the CMB2 plugin to place extra headings and info onto my category pages above the posts. I can get the fields to show in the backend but nothing shows up on the front?

The code I am using to get the info in the backend:

 add_action( 'cmb2_admin_init', 'register_taxonomy_metabox' );
      /**
       * Hook in and add a metabox to add fields to taxonomy terms
       */
      function register_taxonomy_metabox() {
        $prefix = 'cat_headings_';

        /**
         * Metabox to add fields to categories and tags
         */
        $cmb_term = new_cmb2_box( array(
            'id'               => $prefix . 'edit',
            'title'            => esc_html__( 'Category Metabox', 'cmb2' ), // Doesn't output for term boxes
            'object_types'     => array( 'term' ), // Tells CMB2 to use term_meta vs post_meta
            'taxonomies'       => array( 'category' ), // 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__( 'Category Headings', 'cmb2' ),
            'desc'     => esc_html__( 'Sub Heading and Sub Title', 'cmb2' ),
            'id'       => $prefix . 'cat_info',
            'type'     => 'title',
            'on_front' => false,
        ) );

        $cmb_term->add_field( array(
            'name' => esc_html__( 'Category Sub Heading', 'cmb2' ),
            'desc' => esc_html__( 'Sub Heading', 'cmb2' ),
            'id'   => $prefix . 'cat-sub-heading',
            'type' => 'text',
        ) );
      }

and the code that I am using to get the info on the front:

          add_action('genesis_before_loop', 'cat_sub_title', 5 );
      function cat_sub_title($term_id) {
        $catsubhead = get_term_meta( $term_id, 'cat_headings_cat-sub-heading', true );
            if ( is_category() ) {
                if ( $catsubhead ) {
                    printf( '<h2 class="sub-heading">%s</h2>', $catsubhead );
            }
        }
      }

I have been able to echo some html out to the front so I know I'm probably doing something wrong with CMB2 I'm just not sure what? Any would most grateful for any help>

Thanks

1

As from here I can see you used a parameter on your cat_sub_title() function but I don't think you echo manually. you just hooking this function. That's why it's not working.

Try like this. and there is no need to pass any parameter but if you do please working with that.

add_action('genesis_before_loop', 'cat_sub_title', 5 );
function cat_sub_title() {
    $term_id = get_queried_object()->term_id;
    $catsubhead = get_term_meta( $term_id, 'cat_headings_cat-sub-heading', true );
        if ( is_category() ) {
            if ( $catsubhead ) {
                printf( '<h2 class="sub-heading">%s</h2>', $catsubhead );
        }
    }
}

Hope it makes sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.