Support Forum
Support › MB Term Meta › Get CFs from Taxonomy terms - OxygenResolved
Goodmorning,
My goal is to get some MB Custom Fields from a MB Custom Taxonomy for each term I am querying (I want to show the Custom Taxonomy image and do a conditional logic with two Switch).
I tried to integrate the rwmb_get_value function into this PHP code snippet I found on WordPress official documentation but I can't get the custom fields on the frontend (Two Switch and one Advanced Image). The other parts of the script are working correctly: it shows the Custom Taxonomy name and link on the frontend.
Do you have any suggestion to query the Custom Fields? Sorry but I am a newbie and I've just started using MetaBox.
This is the PHP Code
$args = array( 'hide_empty=0' );
$terms = get_terms( 'album-fotografie', $args );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
$count = count( $terms );
$i = 0;
$term_list = '<p class="my_term-archive">';
foreach ( $terms as $term ) {
$i++;
$test=rwmb_get_value( 'album_fotografie_gallerie_scelta', $term->term_id ); //testing rwmb_meta function
echo $test; // not working
$term_list .= '<a href="' . esc_url( get_term_link( $term ) ) . '" alt="' . esc_attr( sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $term->name ) ) . '">' . $term->name . '</a>';
if ( $count != $i ) {
$term_list .= ' · ';
}
else {
$term_list .= '</p>';
}
}
echo $term_list;
}
Thanks in advance,
Francesco
Hi,
Please follow this documentation to know how to output the term meta value on the frontend
https://docs.metabox.io/extensions/mb-term-meta/
$value = rwmb_meta( 'album_fotografie_gallerie_scelta', ['object_type' => 'term'], $term->term_id );
echo $value;
Hi there,
thanks for your help!
I tried your code but even with a simple text field it is not working, do you have any suggestions?
Could it be an Oxygen Builder issue/bug? Is there any possibility to make you have a look to my website MetaBox Code?
Thanks in advance,
Francesco
Hi,
Can you please share the code that creates the custom fields on your site?
Of course, here is the MetaBox PHP code for the taxonomy Custom Fields
<?php
add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' );
function your_prefix_function_name( $meta_boxes ) {
$prefix = '';
$meta_boxes[] = [
'title' => __( 'Album Fotografie Custom Fields', 'your-text-domain' ),
'id' => 'categoria-fotografie-custom-fields',
'taxonomies' => ['album-fotografie'],
'fields' => [
[
'name' => __( 'Album Fotografie - Immagine in evidenza', 'your-text-domain' ),
'id' => $prefix . 'album_fotografie_immagine',
'type' => 'image_advanced',
'max_file_uploads' => 1,
],
[
'name' => __( 'Album Fotografie - Gallerie Scelta', 'your-text-domain' ),
'id' => $prefix . 'album_fotografie_gallerie_scelta',
'type' => 'switch',
'label_description' => __( 'Settare su On/flaggare se l\'album deve comparire nella pagina Gallerie', 'your-text-domain' ),
'style' => 'square',
'on_label' => 'Visibile',
'off_label' => 'Nascosto',
],
[
'name' => __( 'Album Fotografie - Pubblicazioni Scelta', 'your-text-domain' ),
'id' => $prefix . 'album_fotografie_pubblicazioni_scelta',
'type' => 'switch',
'label_description' => __( 'Settare su On/flaggare se l\'album deve comparire nella pagina Pubblicazioni', 'your-text-domain' ),
'style' => 'square',
'on_label' => 'Visibile',
'off_label' => 'Nascosto',
],
[
'name' => __( 'test', 'your-text-domain' ),
'id' => $prefix . 'test',
'type' => 'text',
'std' => '1234test campo personalizzato',
],
],
];
return $meta_boxes;
}
And here is the PHP Code for the Taxonomy
<?php
add_action( 'init', 'your_prefix_register_taxonomy' );
function your_prefix_register_taxonomy() {
$labels = [
'name' => esc_html__( 'Album Fotografie', 'your-textdomain' ),
'singular_name' => esc_html__( 'Album Fotografie', 'your-textdomain' ),
'menu_name' => esc_html__( 'Album Fotografie', 'your-textdomain' ),
'search_items' => esc_html__( 'Search Album Fotografie', 'your-textdomain' ),
'popular_items' => esc_html__( 'Popular Album Fotografie', 'your-textdomain' ),
'all_items' => esc_html__( 'All Album Fotografie', 'your-textdomain' ),
'parent_item' => esc_html__( 'Parent Album Fotografie', 'your-textdomain' ),
'parent_item_colon' => esc_html__( 'Parent Album Fotografie:', 'your-textdomain' ),
'edit_item' => esc_html__( 'Edit Album Fotografie', 'your-textdomain' ),
'view_item' => esc_html__( 'View Album Fotografie', 'your-textdomain' ),
'update_item' => esc_html__( 'Update Album Fotografie', 'your-textdomain' ),
'add_new_item' => esc_html__( 'Add New Album Fotografie', 'your-textdomain' ),
'new_item_name' => esc_html__( 'New Album Fotografie Name', 'your-textdomain' ),
'separate_items_with_commas' => esc_html__( 'Separate album fotografie with commas', 'your-textdomain' ),
'add_or_remove_items' => esc_html__( 'Add or remove album fotografie', 'your-textdomain' ),
'choose_from_most_used' => esc_html__( 'Choose most used album fotografie', 'your-textdomain' ),
'not_found' => esc_html__( 'No album fotografie found.', 'your-textdomain' ),
'no_terms' => esc_html__( 'No album fotografie', 'your-textdomain' ),
'filter_by_item' => esc_html__( 'Filter by album fotografie', 'your-textdomain' ),
'items_list_navigation' => esc_html__( 'Album Fotografie list pagination', 'your-textdomain' ),
'items_list' => esc_html__( 'Album Fotografie list', 'your-textdomain' ),
'most_used' => esc_html__( 'Most Used', 'your-textdomain' ),
'back_to_items' => esc_html__( '← Go to Album Fotografie', 'your-textdomain' ),
'text_domain' => esc_html__( 'your-textdomain', 'your-textdomain' ),
];
$args = [
'label' => esc_html__( 'Album Fotografie', 'your-textdomain' ),
'labels' => $labels,
'description' => '',
'public' => true,
'publicly_queryable' => true,
'hierarchical' => false,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_rest' => true,
'show_tagcloud' => true,
'show_in_quick_edit' => true,
'show_admin_column' => false,
'query_var' => true,
'sort' => false,
'meta_box_cb' => 'post_tags_meta_box',
'rest_base' => '',
'rewrite' => [
'with_front' => false,
'hierarchical' => false,
],
];
register_taxonomy( 'album-fotografie', ['fotografie'], $args );
}
My goal is to show on the frontend the Advanced Image and use the two switch inside the PHP code to create something like a conditional logic, but I am having issues also using the two switch, I can't query them.
Hi,
It looks like the arguments passed to the get_terms() function are not correct. It should be
$args = array(
'taxonomy' => 'album-fotografie',
'hide_empty' => 0,
);
$terms = get_terms( $args );
This works as well on my demo site. Please read more on the documentation https://developer.wordpress.org/reference/functions/get_terms/
Thank you, now it works fine!
Best regards,
Francesco