admin管理员组文章数量:1289412
I have a custom taxonomy with custom fields which are being populated with CMB2. I can't seem to figure out why I am unable to populate this information on the frontend.
Below is the code that is generating the custom fields on my custom taxonomy called Placement.
add_action( 'cmb2_admin_init', 'placement_register_taxonomy_metabox' );
function placement_register_taxonomy_metabox() {
$prefix = 'placement_';
$cmb_term = new_cmb2_box( array(
'id' => $prefix . 'placement',
'title' => esc_html__( 'Title Handler', 'veruscref-theme' ), // Doesn't output for term boxes
'object_types' => array( 'term' ), // Tells CMB2 to use term_meta vs post_meta
'taxonomies' => array( 'placement' ), // 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__( 'Loan Program Title', 'veruscref-theme' ),
'desc' => esc_html__( 'Will be displayed on transaction page', 'veruscref-theme' ),
'id' => $prefix . 'tax_header',
'type' => 'title',
'on_front' => false,
) );
$cmb_term->add_field( array(
'name' => esc_html__( 'Title', 'veruscref-theme' ),
'id' => $prefix . 'tax_title',
'type' => 'text',
) );
$cmb_term->add_field( array(
'name' => esc_html__( 'Small Title', 'veruscref-theme' ),
'id' => $prefix . 'tax_small_title',
'type' => 'text',
) );
Below is my frontend code
<?php
$terms = get_terms(
array(
'taxonomy' => 'placement',
'hide_empty' => 1,
'exclude' => array(4,23),
)
);
if ( ! empty( $terms ) && is_array( $terms ) ) { foreach ( $terms as $term ) { ?>
<?php
$title = get_term_meta( get_queried_object_id(), 'placement_tax_title', true );
echo $title;
?>
<?php } } ?>
What am I doing wrong with my code?
I have a custom taxonomy with custom fields which are being populated with CMB2. I can't seem to figure out why I am unable to populate this information on the frontend.
Below is the code that is generating the custom fields on my custom taxonomy called Placement.
add_action( 'cmb2_admin_init', 'placement_register_taxonomy_metabox' );
function placement_register_taxonomy_metabox() {
$prefix = 'placement_';
$cmb_term = new_cmb2_box( array(
'id' => $prefix . 'placement',
'title' => esc_html__( 'Title Handler', 'veruscref-theme' ), // Doesn't output for term boxes
'object_types' => array( 'term' ), // Tells CMB2 to use term_meta vs post_meta
'taxonomies' => array( 'placement' ), // 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__( 'Loan Program Title', 'veruscref-theme' ),
'desc' => esc_html__( 'Will be displayed on transaction page', 'veruscref-theme' ),
'id' => $prefix . 'tax_header',
'type' => 'title',
'on_front' => false,
) );
$cmb_term->add_field( array(
'name' => esc_html__( 'Title', 'veruscref-theme' ),
'id' => $prefix . 'tax_title',
'type' => 'text',
) );
$cmb_term->add_field( array(
'name' => esc_html__( 'Small Title', 'veruscref-theme' ),
'id' => $prefix . 'tax_small_title',
'type' => 'text',
) );
Below is my frontend code
<?php
$terms = get_terms(
array(
'taxonomy' => 'placement',
'hide_empty' => 1,
'exclude' => array(4,23),
)
);
if ( ! empty( $terms ) && is_array( $terms ) ) { foreach ( $terms as $term ) { ?>
<?php
$title = get_term_meta( get_queried_object_id(), 'placement_tax_title', true );
echo $title;
?>
<?php } } ?>
What am I doing wrong with my code?
Share Improve this question edited Jun 29, 2021 at 19:18 user3756781 asked Jun 29, 2021 at 19:08 user3756781user3756781 1793 silver badges17 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 0You need to use $term->term_id
not get_queried_object_id()
in get_term_meta
本文标签: phpHow to retrieve taxonomy Metabox fields in frontend
版权声明:本文标题:php - How to retrieve taxonomy Metabox fields in frontend 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741475435a2380854.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
get_queried_object_id()
will only return a term's ID if you're looking at a term archive - is that what you intend? – bosco Commented Jun 29, 2021 at 19:39get_queried_object_id
instead of$term->ID
? – Tom J Nowell ♦ Commented Jun 29, 2021 at 19:42term_id
, I'm unsure of the exact name but the$term
object contains the ID of the term – Tom J Nowell ♦ Commented Jun 29, 2021 at 20:01