Support » Plugin: CMB2 » How to display Category Metabox fields in frontend ?

  • Resolved abderr

    (@abderr)


    Hello,
    this is my CMB2 Code :

    <?php 
    
    if ( file_exists( dirname( __FILE__ ) . '/CMB2/init.php' ) ) {
    	require_once dirname( __FILE__ ) . '/CMB2/init.php';
    } 
    
    add_action( 'cmb2_admin_init', 'tw_term_meta' );
    /**
     * Define the metabox and field configurations.
     */
    function tw_term_meta() {
    
    	/**
    	 * Initiate the metabox
    	 */
        $prefix = 'tw_term_';
    	$cmb = new_cmb2_box( array(
    		'id'            => $prefix . 'tw_hero_meta',
    		'title'         => __( 'Hero', 'tw' ),
    		'object_types'     => array( 'term' ), // Tells CMB2 to use term_meta vs post_meta 
     		'taxonomies'       => array( 'category' ),
    		'context'       => 'normal',
    		'priority'      => 'high',
    		'show_names'    => true, // Show field names on the left
    		// 'cmb_styles' => false, // false to disable the CMB stylesheet
    		// 'closed'     => true, // Keep the metabox closed by default
    	) );
    
    	// File Upload field
    	$cmb->add_field( array(
    		'name'       => __( 'Background :', 'tw' ),
    		'desc'       => __( 'image de fond qui apparaîtra au hero (Upload an image or enter a URL).', 'tw' ),
    		'id'         => $prefix . 'background',
    		'type'       => 'file',
    		'show_on_cb' => 'cmb2_hide_if_no_cats', // function should return a bool value
    		// 'sanitization_cb' => 'my_custom_sanitization', // custom sanitization callback parameter
    		// 'escape_cb'       => 'my_custom_escaping',  // custom escaping callback parameter
    		// 'on_front'        => false, // Optionally designate a field to wp-admin only
    		// 'repeatable'      => true,
    	) );
    	$cmb->add_field( array(
    		'name'    => 'Titre',
    		'desc'    => 'titre du hero (optional)',
    		//'default' => 'standard value (optional)',
    		'id'      => $prefix . 'main_title',
    		'type'    => 'text',
    	) );
    	$cmb->add_field( array(
    		'name'    => 'Short description',
    		'desc'    => 'Sous titre du hero (optional)',
    		//'default' => 'standard value (optional)',
    		'id'      => $prefix . 'short_desc',
    		'type'    => 'textarea',
    	) );	
        $cmb->add_field( array(
            'name' => 'Bouton CTA contact',
            'desc' => 'Activer bouton qui renvoie à contact.',
            'id'   => $prefix . 'buttoncta',
            'type' => 'checkbox',
    	) );
    	
    	$cmb->add_field( array(
            'name' => 'Bouton CTA newsletter',
            'desc' => 'Activer bouton qui renvoie à newsletter.',
            'id'   => $prefix . 'buttonnewsletter',
            'type' => 'checkbox',
        ) );
    
    }
    
    

    and this is my code in category.php

    <div class="banner-area">
            <div class="bg-banner" style="background-image: url(<?php echo get_term_meta( get_the_ID(), 'tw_term_background', true );?>)"></div>
            <div class="container">
                <div class="headline">
                    <?php $maintitle = get_term_meta( get_the_ID(), 'tw_term_main_title', true );
                    ?>
                    <h1><?php echo $maintitle; ?></h1>
                    
                      
                </div>
            </div>
        </div>

    where is the probleme?

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