Show custom Fields on a list of Taxonomy terms

Support MB Term Meta Show custom Fields on a list of Taxonomy termsResolved

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #32314
    KnütKnüt
    Participant

    Hello!

    I need to display a list of terms from a Taxonomy of a Custom Post Type with some data of a Custom Field.

    From now on I have set up the CPT (article) with the Taxonomy (butlleti) and created a bunch of terms for this Taxonomy. I also created two Custom Fields (id 'mes_butlleti' and 'any_butlleti') for that Taxonomy (butlleti).

    Then, I managed to have this code that creates the list of terms:

    <?php
    $terms = get_terms( array(
    'taxonomy' => 'butlleti', // set your taxonomy here
    'orderby' => 'date', // default: 'orderby' => 'name',
    'order' => 'DESC',
    'hide_empty' => false, // default: true
    ) );
    if ( empty( $terms ) || is_wp_error( $terms ) ) {
    return;
    }
    echo '<div class="butlletins__list-container">';
    foreach( $terms as $term ) {
    printf(
    '<a href="%s">
        <div class="butlletins__list">
            <p class="p-big">ARTICLE N. <strong>%s</strong></p>
            <div class="butlletins__list-line"></div>
            <p> HERE I NEED TO PRINT THE TWO CUSTOM FIELDS </p>
        </div>
    </a>',
    esc_url( get_term_link( $term ) ),
    esc_attr( $term->name )
    );
    }
    echo '</div>';
    ?>

    I would like to print the data from both of the Custom Fields associated to the Taxonomy (butlleti) for each term.

    Is that possible? Can you give me some light?

    Thank you very much!!

    #32327
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Please follow this topic to show the term meta https://support.metabox.io/topic/taxonomy-image-on-custom-post-type/

    and read more on the documentation https://docs.metabox.io/extensions/mb-term-meta/

    #32333
    KnütKnüt
    Participant

    Hi Long Nguyen,

    Thanks for your help!

    I've been reading your documentation, but I can't figure it out how to make it work with the code I already have to show both the Terms and a Custom Field of that term.

    Could you please help me figure it out?

    Thank you!

    #32344
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Here is an example to show the term meta in the loop

    foreach ( $terms as $term ) {
        $mes_butlleti= rwmb_meta( 'mes_butlleti', ['object_type' => 'term'], $term->term_id );
        echo $mes_butlleti; 
    }
    #32354
    KnütKnüt
    Participant

    Hi,

    That's great!

    Do you know how I can print $mes_butlleti and $any_butlleti into the HTML below?

    I would like to have those term data where it says <p>HERE I NEED TO PRINT THE mes_butlleti AND any_butlleti DATA<p>

    <?php
    $terms = get_terms( array(
    'taxonomy' => 'butlleti', // set your taxonomy here
    'orderby' => 'none', // default: 'orderby' => 'name',
    'order' => 'ASC',
    'hide_empty' => true, // default: true
    'number' => 1,
    ));
    if ( empty( $terms ) || is_wp_error( $terms ) ) {
    return;
    }
    echo '<div class="butlletins__list-container">';
    foreach( $terms as $term ) {
        $mes_butlleti= rwmb_meta( 'mes_butlleti', ['object_type' => 'term'], $term->term_id );
        $any_butlleti= rwmb_meta( 'any_butlleti', ['object_type' => 'term'], $term->term_id );
        echo $mes_butlleti;
        echo $any_butlleti;
    printf(
    '<a href="%s">
        <div class="butlletins__list">
            <p class="p-big">BUTLLETÍ N. <strong>%s</strong></p>
            <div class="butlletins__list-line"></div>
            <p>HERE I NEED TO PRINT THE mes_butlleti AND any_butlleti DATA<p>
            <span class="butlletins__list-new">NOU</span>
        </div>
    </a>',
    esc_url( get_term_link( $term ) ),
    esc_attr( $term->name ),
    $term->count
    );
    }
    echo '</div>';
    
    ?>

    Thanks!!!

    #32355
    KnütKnüt
    Participant

    Hi!

    I rewrite all the code using echo, not printf, and now it works fine!

    Thanks for your help!

    #45980
    Enrico FranciEnrico Franci
    Participant

    Hello I tried to get something like to be have a list of all the taxonomy list in a sidebar widget but I get only a blank space of the item in hmtl content space.

    <?php
    $terms = get_terms( array(
    'taxonomy' => 'categorie', 
    'orderby' => 'name', 
    'order' => 'ASC',
    'hide_empty' => true, 
    ));
    if ( empty( $terms ) || is_wp_error( $terms ) ) {
    return;
    }
    foreach( $terms as $term ) {
        $cat_title= rwmb_meta( 'categorie', ['object_type' => 'term'], $term->name );
    	echo '<ul><li>';
        echo'<a href="'. get_term_link( $term ) .'">'. $cat_title . '</a>';
        echo '</li></ul>';
    }
    ?>
    

    https://www.maxxdesign.it/gremesefr/livre/ghost/

    Can you explain me where is the bug in my code?

    #45989
    Enrico FranciEnrico Franci
    Participant

    Solved by this function...

    function wpdocs_custom_categories_terms_links() {
    	// Get post by post ID.
    	if ( ! $post = get_post() ) {
    		return '';
    	}
    
    	// Get post type by post.
    	$post_type = $post->post_type;
    
    	// Get post type taxonomies.
    	$taxonomies = get_object_taxonomies( $post_type, 'livre' );
    
    	$out = array();
    
    	foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
    
    		// Get the terms related to post.
    		$taxonomy = 'categorie';
    		$terms = get_the_terms( $post->ID, $taxonomy_slug );
    
    		if ( ! empty( $terms ) ) {
    			$out[] = "<ul>";
    			foreach ( $terms as $term ) {
    				$out[] = sprintf( '<li class="cat"><a href="%1$s">%2$s</a></li>',
    					esc_url( get_term_link( $term->slug, $taxonomy_slug ) ),
    					esc_html( $term->name )
    				);
    			}
    			$out[] = "\n</ul>\n";
    		}
    	}
    	return implode( '', $out );
    }
    
    #45991
    Enrico FranciEnrico Franci
    Participant

    Not solved because in the sidebar I get the list of the specific post and not the complete list of registered taxanomy terms!

    #45992
    Enrico FranciEnrico Franci
    Participant

    Solved with a flexible shortcode solution

    function list_terms_custom_taxonomy( $atts ) {
      
    // Inside the function we extract custom taxonomy parameter of our shortcode
      
        extract( shortcode_atts( array(
            'custom_taxonomy' => '',
       ), $atts ) );
      
    // arguments for function wp_list_categories
    $args = array( 
    'taxonomy' => $custom_taxonomy,
    'title_li' => ''
    );
      
    if  ($custom_taxonomy = 'categorie') {
    	$widget_title = 'Catégories';
    	}
    else if 
        ($custom_taxonomy = 'collection') {
    	$widget_title = 'Collections';
        }
        
    // We wrap it in unordered list 
    
    echo '<h2 class="widget-title">' .$widget_title. '</h2><ul class="cat-list">'; 
    echo wp_list_categories($args);
    echo '</ul>';
    }
      
    // Add a shortcode that executes our function
    add_shortcode( 'ct_terms', 'list_terms_custom_taxonomy' );
      
    //Allow Text widgets to execute shortcodes
      
    add_filter('widget_text', 'do_shortcode');
    
Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.