我在自定义主题中使用了CMB2,我希望使用CMB2在默认类别页面上传自定义图像。
文本字段正确显示在正面。但是wp_get_attachment_image
无效。这有什么不对的?
CMB元框寄存器:
add_action( 'cmb2_admin_init', 'term_metabox_register' );
function term_metabox_register() {
$prefix = 'my_term_';
$cat_field = new_cmb2_box( array(
'id' => $prefix . 'term_metabox',
'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' => $prefix . 'file',
'type' => 'file',
) );
$cat_field->add_field( array(
'name' => 'Custom Text',
'desc' => 'Custom description.',
'id' => $prefix . 'custom_text',
'type' => 'text',
) );
}
输出:
$categories = get_the_category();
$category_id = $categories[0]->cat_ID;
echo get_term_meta( $category_id, 'my_term_custom_text', 1 ); // This works.
echo wp_get_attachment_image( get_term_meta( $category_id, 'my_term_file', 1 ), 'large' ); // Not works.