Custom Fields
Re-Envisioned
Support MB Custom Taxonomy Oxygen - list custom taxonomy in archive page

  • Creator
    Topic
  • #27711
    Resolved Heave
    Participant

    So we recently came from acf to metabox.

    We were using the folowing code to list all category o a post inside an archive page :

    <?php
    // your taxonomy name
    $tax = 'propriete';
    
    // get the terms of taxonomy
    $terms = get_terms( $tax, $args = array(
      'hide_empty' => false, // do not hide empty terms
    ));
    
    ?><div class="pop"><?php
    // loop through all terms
    foreach( $terms as $term ) {
    
    if( $term->count > 0 )
        // display link to term archive
        echo '<span class="cat-bg">'. $term->name . ', '. '</span>';
    
    }
    ?></div>
    

    But since we use metabox, the code list all the custom taxonomy available, not only the one the post is in.

    Need you light over here 🙂

Viewing 9 replies - 1 through 9 (of 9 total)
  • Author
    Replies
  • #27722
    Long Nguyen
    Moderator

    Hi,

    I do not see any issue with your code. Can you please share the code that creates the taxonomy? I will check it on my local site. Or you can try to get terms with the new type.

    $terms = get_terms( array(
        'taxonomy' => 'propriete',
        'hide_empty' => false,
    ) );

    Get more details here https://developer.wordpress.org/reference/functions/get_terms/

    #27723
    Heave
    Participant

    Hi there,
    Taxonomy is directly created with metabox builder.

    We need to list without linking to the category

    #27747
    Long Nguyen
    Moderator

    Hi,

    As I understand, you want to show the terms of the taxonomy propriete on the archive page. But the code outputs all taxonomies. Did you try to use this code?

    $terms = get_terms( array(
        'taxonomy' => 'propriete',
        'hide_empty' => false,
    ) );
    #27756
    Heave
    Participant

    All the terms of "propriete"
    I just want to display the one attached to each post on the grid

    #27758
    Heave
    Participant

    like that ?
    <?php
    // your taxonomy name
    $tax = 'propriete';

    // get the terms of taxonomy
    $terms = get_terms( array(
    'taxonomy' => 'propriete',
    'hide_empty' => false,
    ) );

    ?><div class="pop"><?php
    // loop through all terms
    foreach( $terms as $term ) {

    if( $term-&gt;count &gt; 0 )
        // display link to term archive
        echo '&lt;span class="cat-bg"&gt;'. $term-&gt;name . ', '. '&lt;/span&gt;';
    

    }
    ?></div>

    #27776
    Heave
    Participant

    also tried using

    $terms = rwmb_meta( 'propriete' );
    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
    foreach ( $terms as $term ) {
    echo '<p>', $term->name, '</p>';
    }
    }

    But nothing 🙁

    #27801
    Long Nguyen
    Moderator

    Hi,

    To show the terms of each post, please use the function get_the_terms() or wp_get_post_terms().

    #27806
    Heave
    Participant

    This is exactly what i was using, but it list all terms of the taxonomy propriete, not only the ones connextected to one's post...

    <?php
    // your taxonomy name
    $tax = 'propriete';
    
    // get the terms of taxonomy
    $terms = get_terms( $tax, $args = array(
      'hide_empty' => false, // do not hide empty terms
    ));
    
    ?><div class="pop"><?php
    // loop through all terms
    foreach( $terms as $term ) {
    
    if( $term->count > 0 )
        // display link to term archive
        echo '<span class="cat-bg">'. $term->name . ', '. '</span>';
    
    }
    ?></div>
    #27818
    Long Nguyen
    Moderator

    Hi,

    Your code need to use inside a loop, just like

    while ( have_posts() ) {
        the_post();
    
        // your taxonomy name
        $tax = 'propriete';
    
        // get the terms of taxonomy
        $terms = wp_get_post_terms( $post->ID, $tax, array( 'hide_empty' => false ) );
    
        // loop through all terms
        foreach( $terms as $term ) {
    
        if( $term->count > 0 )
            // display link to term archive
            echo '<span class="cat-bg">'. $term->name . ', '. '</span>';
    
        }
    }
Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.