Support

Account

Home Forums General Issues Custom Taxonomy Archive Issues

Solved

Custom Taxonomy Archive Issues

  • Hello, I am having trouble getting the field values to display on my taxonomy archive (archive-mytax.php). The archive shows a list of my tax terms using this.

    <?php
    
    $terms = get_terms( 'mytax' , 'hide_empty=false' );
    
    echo '<div class="row">';
    
    foreach ( $terms as $term ) {
    
        // The $term is an object, so we don't need to specify the $taxonomy.
        $term_link = get_term_link( $term );
    
        // If there was an error, continue to the next term.
        if ( is_wp_error( $term_link ) ) {
            continue;
        }
    
        // We successfully got a link. Print it out.
        echo '<div class="col-lg-3"><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></div>';
    }
    
    echo '</div>';
    
    ?>

    I have set up custom fields for my taxonomy terms. Am having trouble using the suggested codes (any of them) to return the field values.

  • Are you using something that looks like this:

    
    $id = $term->slug.'_'.$term->term_id;
    get_field('custom_field_name', $id);
    

    As shown on http://www.advancedcustomfields.com/resources/functions/get_field/ the id needed to get your custom field is a combo of the term slug and underscore and the term id.

  • Hi I have tried this a few ways, but I don’t think I’m doing it correct. :S I’m not a PHP guru. Can you help me know where exactly my values need to go in that code? Assume my taxonomy is “Research”. Also, I really want to avoid specifying an actual term ID. I would like the fields to be pulled automatically for every term displaying on my archive page.

  • The term_id is drawn from the term in your loop of the results from get_terms. WP does not automatically generate the id that you need. These are special IDs that ACF needs to locate your data.

    
    foreach ( $terms as $term ) {
    
        // The $term is an object, so we don't need to specify the $taxonomy.
        $term_link = get_term_link( $term );
    
        // If there was an error, continue to the next term.
        if ( is_wp_error( $term_link ) ) {
            continue;
        }
    
        // We successfully got a link. Print it out.
        echo '<div class="col-lg-3"><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></div>';
    
        // show custom fields for the term
        // this id will be generated for each term
        $id = $term->slug.'_'.$term->term_id;
        the_field('my_first_custom_field', $id);
        the_field('my_second_custom_field', $id);
    }
    
  • Do I have to change anything on this line?

    
    $id = $term->slug.'_'.$term->term_id;
  • you shouldn’t need to as long as it’s inside your loop

    
    foreach ( $terms as $term ) {
      $id = $term->slug.'_'.$term->term_id;
    }
    
  • Hm, well I couldn’t make that work, but found another solution online that seems to do what I need.

    <?php
    
            $libargs=array(  
                'hide_empty'        => 0,  
                'parent'        => 0,  
                'taxonomy'      => 'MYTAXONOMYNAME');  
    			
    
                $libcats=get_categories($libargs);  
    
                foreach($libcats as $lc){ 
    			echo '<div class="col-lg-3">';
                    $termlink = get_term_link( $lc->slug, 'MYTAXONOMYNAME' ); 
    
            ?>
               <p class="text-center"> <a href="<?php echo $termlink; ?>">
     <img class="img-responsive img-thumbnail" src="<?php the_field('taximage', 'MYTAXONOMYNAME_'.$lc->term_id); ?>" />
       <br />
                <?php echo $lc->name; ?></a></p>
                         <?php the_field('basictext', 'MYTAXONOMYNAME_'.$lc->term_id); ?>
                         
                          
                   <?php   echo '</div>'; }?>

    Not real sure why it works over the other method, but it does.

  • okay, sorry, I didn’t do a test and after looking at the code I realize my mistake.

    The first part where I put “$term->term_slug” should have been the “Taxonomy Slug” for your taxonomy.
    'my-tax_'.$term->term_id
    which is, of course, what that code you posted is doing. My brain is just turning to mush 😛

  • Hi Hube! Well thanks for letting me know why it wasn’t working. I’m somewhat glad to know it wasn’t just me. I’m still a learning. 🙂 Thanks again!

Viewing 9 posts - 1 through 9 (of 9 total)

The topic ‘Custom Taxonomy Archive Issues’ is closed to new replies.