Support » Plugin: CMB2 » Custom Taxonomy Image on front-end

  • Resolved EliWbbr

    (@eliwbbr)


    i created a custom-post-type called “products” which has a custom-taxonomy called “department”.

    in that taxonomy is an ‘add image’ metabox set up for users to add an image to represent the department on the front-end.

    can you help me get it to display the front-end? i can’t seem to make it happen.

    they should show on this page: http://staging10.thestudio-patrick.com/products/

    here is my page-products.php code:

    <div class="row">					
    <?php
    	$terms = get_terms('department'); 
    		foreach ( $terms as $term ) {
    			echo '<div class="col-sm-12 col-md-3">';
    
    				echo '<a href="/department/' . $term->slug . '">' . $term->name . '</a>';
    
    				echo '<div style="border:1px solid blue;">';
    					//$file = wp_get_attachment_image( get_post_meta( get_the_ID(), '_cmb2_term_dept_img_id', 1 ), '' );
    					$file = get_post_meta( $post->ID, '_cmb2_term_dept_img', true );
    					echo $file;
    				echo '</div>';
    
    				echo '<div style="border:1px solid yellow;">';
    					$file = get_post_meta( get_the_ID(), '_cmb2_term_dept_img', true );
    					//$file = wp_get_attachment_image( get_post_meta( get_the_ID(), '_cmb2_term_dept_img_id', 1 ), '' );
    					foreach ( (array) $file as $attachment_id => $attachment_url ) {
    						echo '<div id="gallery" class="gallery gallery-size-thumbnail">';
    							echo '<figure class="gallery-item">';
    								echo '<div class="gallery-icon landscape">';
    									echo '<a rel="prettyPhoto[pp_gal]" href=" ';
    										echo $attachment_url;
    										echo '">';
    										echo wp_get_attachment_image( $attachment_id );
    									echo '</a>';
    								echo '</div>';
    							echo '</figure>';
    						echo '</div>';
    					}	
    				echo '</div>'; // closes yellow border div
    
    			echo "</div>";// closes .col-sm-12 and .col-md-3 div
    	}	
    ?>
    </div><!-- eof: .row -->

    and here is my code from my metabox-functions file:

    
    <?php
    /**
     * Get the bootstrap!
     */
    
    if ( file_exists( dirname( __FILE__ ) . '/cmb2/init.php' ) ) {
    	require_once dirname( __FILE__ ) . '/cmb2/init.php';
    } elseif ( file_exists( dirname( __FILE__ ) . '/CMB2/init.php' ) ) {
    	require_once dirname( __FILE__ ) . '/CMB2/init.php';
    }
    
    add_action( 'cmb2_init', 'cmb2_metaboxes' );
    
    /**
     * Define the metabox and field configurations.
     */
    function cmb2_metaboxes() {
    
    // Start with an underscore to hide fields from custom fields list
    $prefix = '_cmb2_';
    
    /**
     * Initiate PRODUCTS metabox
     */
    $cmb = new_cmb2_box( array(
    	'id'            => 'products_metabox',
    	'title'         => __( 'Products', 'cmb2' ),
    	'object_types'  => array( 'post', 'product' ), // Post type
    	'context'       => 'normal',
    	'priority'      => 'high',
    	'show_names'    => true, // Show field names on the left
    	'cmb_styles' 	=> true, // false to disable the CMB stylesheet
    	// 'closed'     => true, // Keep the metabox closed by default
    ) );
    		$cmb->add_field ( array(
    				'name'         => __( 'Product Number', 'cmb2' ),
    				'desc'         => __( 'Enter a product number for this product', 'cmb2' ),
    				'id'           => $prefix . 'product_number',
    				'type'         => 'text',
    				'column' => array( 'position' => 4 ),
    		) );
    		$cmb->add_field( array(
    				'name'         => __( 'Product Image(s)', 'cmb2' ),
    				'desc'         => __( 'Upload an image or images of this product.', 'cmb2' ),
    				'id'           => $prefix . 'productimages_file_list',
    				'type'         => 'file_list',
    				'preview_size' => array( 100, 100 ), // Default: array( 50, 50 )
    		) );				
    		$cmb->add_field( array(
    				'name'         => __( 'Spec Sheet', 'cmb2' ),
    				'desc'         => __( 'Upload the product spec sheet or attachment', 'cmb2' ),
    				'id'           => $prefix . 'specsheet_file_list',
    				'type'         => 'file_list',
    				'preview_size' => array( 100, 100 ), // Default: array( 50, 50 )
    		) );						
    		$cmb->add_field( array(
    				'name' 		   => esc_html__( 'Price', 'cmb2' ),
    				'desc' 		   => esc_html__( 'field description (optional)', 'cmb2' ),
    				'id'   		   => $prefix . 'textmoney',
    				'type' 		   => 'text_money',
    				'before_field' => '$', // override '$' symbol if needed
    				// 'repeatable' => true,
    		) );
    
        // Add other metaboxes as needed
    
    }
    
    add_action( 'cmb2_init', 'cmb2_register_taxonomy_metabox' );
    /**
     * Hook in and add a metabox to add fields to taxonomy terms
     */
    function cmb2_register_taxonomy_metabox() {
    	$prefix = '_cmb2_term_';
    
    /**
     * Metabox to add fields to categories and tags
     */
    $cmb_term = new_cmb2_box( array(
    	'id'               => $prefix . 'edit',
    	'title'            => esc_html__( 'Department Image', 'cmb2' ), // Doesn't output for term boxes
    	'object_types'     => array( 'term' ), // Tells CMB2 to use term_meta vs post_meta
    	'taxonomies'       => array( 'department', 'post_tag' ), // 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__( 'Department Image', 'cmb2' ),
    			'desc' 			=> esc_html__( 'Add an image to represent this department', 'cmb2' ),
    			'id'   			=> $prefix . 'dept_img',
    			'type' 			=> 'file',
    			'column' 		=> array( 'position' => 8 ),
    		) );
    	// Add other metaboxes as needed
    }
    

    really hoping someone can point me in the right direction

Viewing 2 replies - 16 through 17 (of 17 total)
  • Thread Starter EliWbbr

    (@eliwbbr)

    Hi Michael-
    Here is it is in pastebin:
    https://pastebin.com/vbyh3pyk

    (is this what you meant? its my first time using it)

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Yeah, that one looks right.

    Looks like the output has changed to just 1 term at the moment. My hunch is maybe the last time we had a case of 3 posts, that all had the same 3 terms assigned to each.

    Outputting the post title and perhaps some styling to separate out each post would help visualize things a lot more.

    That said, I also think we’re in a good position as a whole for you to run with things. We have it showing the right parts at least.

Viewing 2 replies - 16 through 17 (of 17 total)
  • The topic ‘Custom Taxonomy Image on front-end’ is closed to new replies.