Sunday, October 16, 2022
HomeWordPress Developmentphp - Displaying Class Function picture on loop

php – Displaying Class Function picture on loop


I used CMB2 to made characteristic picture for class. The characteristic picture working completely on Class web page.
I have to show record of class on different web page, with that characteristic picture, however the picture didn’t present up.

CMB meta field register:

 add_action( 'cmb2_admin_init', 'term_metabox_register' );
 perform term_metabox_register() {
 $cat_field = new_cmb2_box( array(
    'id'            => 'catimage',
    'title'         => esc_attr__( 'Class Choices', 'text-domain' ),
    'object_types'     => array( 'time period' ), // Tells CMB2 to make use of term_meta vs post_meta
    'taxonomies'       => array( 'class' ), 
) );

$cat_field->add_field( array(
    'title'             => 'Background Picture',
    'desc'             => 'Add your background picture.',
    'id'          => 'catfit',
    'kind'             => 'file',
) );

}

Code on class web page (working completely)

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

Code for the loop to show on different web page (not working, the characteristic picture didn’t present up)

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

What’s improper goes right here? Thanks for any assist.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments