plugins – Displaying Category Feature image on loop

Question

used CMB2 to made feature image for category. The feature image working perfectly on Category page.
I need to display list of category on other page, with that feature image, but the image did not show up.

CMB meta box register:

 add_action( 'cmb2_admin_init', 'term_metabox_register' );
 function term_metabox_register() {
 $cat_field = new_cmb2_box( array(
    'id'            => 'catimage',
    'title'         => esc_attr__( 'Category Options', 'text-domain' ),
    'object_types'     => array( 'term' ), // Tells CMB2 to use term_meta vs post_meta
    'taxonomies'       => array( 'category' ), 
) );

$cat_field->add_field( array(
    'name'             => 'Background Image',
    'desc'             => 'Upload your background image.',
    'id'          => 'catfit',
    'type'             => 'file',
) );

}

Code on category page (working perfectly)

<?php
$categories = get_the_category();
$category_id = $categories[0]->cat_ID;
echo wp_get_attachment_image( get_term_meta( $category_id, 'catfit_id', 1 ), 'medium-thumbnail' );
?>

Code for the loop to display on other page (not working, the feature image did not show up)

 <?php
 $categories = get_categories( array(
'orderby' => 'name',
'order'   => 'ASC',
'show_count' => false,  
'number'    => 3,       
 ) );
 foreach( $categories as $category ) {
    $category_id = $categories[0]->cat_ID;
    echo '<p>'.$category->name.'</p> ';
    echo wp_get_attachment_image( get_term_meta( $category_id, 'catfit_id', 1 ), 'large' );} ?>

What’s wrong goes here? Thank you for any help.

0
Chl Lila 2 months 2022-10-16T18:34:52-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse