Support

Account

Home Forums Front-end Issues Custom taxonomy acf value in loop on other pages

Solved

Custom taxonomy acf value in loop on other pages

  • Hi. Viewed lots of topics, but still didn’t found an answer.

    So, I have custom post type “flat4” and taxonomy “floor4”. On Front page I’m calling get_categories() and looping through taxonomy to get link and other stuff. I have acf to this taxonomy, and I need to output it in loop. Found this code: <?php the_field(‘hover’,’floor4_’ . $cat->term_id); ?>, but it’s not working.

    Can you please help me with it?

  • What about:

    <?php 
    $term = get_field(‘hover’,’floor4_’ . $cat->term_id);
    echo $term;
    

    Code untested and assume $cat->term_id has a value

  • if you echo $cat->term_id; does it show a value? If so, is it the right one?

    I assume ‘hover’ is the taxonomy name?

  • yes, echo $cat->term_id; returns correct value.
    ‘hover’ – is an acf field name, ‘floor4’ – a taxonomy which has a ‘hover’ value

  • What code do you have?

    Maybe I’ve misunderstood.

    This loops categories:

    <?php
    $categories = get_categories( array(
        'orderby' => 'name',
        'order'   => 'ASC'
    ) );
     
    foreach( $categories as $category ) {
        $category_link = sprintf( 
            '<a href="%1$s" alt="%2$s">%3$s</a>',
            esc_url( get_category_link( $category->term_id ) ),
            esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ),
            esc_html( $category->name )
        );
         
        echo '<p>' . sprintf( esc_html__( 'Category: %s', 'textdomain' ), $category_link ) . '</p> ';
        echo '<p>' . sprintf( esc_html__( 'Description: %s', 'textdomain' ), $category->description ) . '</p>';
        echo '<p>' . sprintf( esc_html__( 'Post Count: %s', 'textdomain' ), $category->count ) . '</p>';
    } 

    This loops taxonomies:

    <?php if( $categories = get_terms( array( 'taxonomy' => 'floor4' ) ) ) :
    foreach( $categories as $cat) : 
    	$hover= get_field('hover', 'term_' .$cat->term_id  );?>
    	<?php echo $hover; ?>
    <?php endforeach;      
    endif;
    ?>
  • i used get_categories(), changed to get_terms(), but still get_field(‘hover’, ‘term_’ .$cat->term_id ) doesn’t return any value

  • Can you show your code please?

    And to clarify, the taxonomy is called floor4
    This is linked to a CPT of flat4
    The ACF field is called hover and it’s this value you’re trying to get?

    Does this work:

    global $wp_query;
    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    $args = array(
    	'posts_per_page' => -1,
    	'post_type'		=> 'flat4',
    	'paged' 		=> $paged,
    	'fields' 		=> 'ids'			
    );
    $wp_query = new WP_Query($args);
    if ($wp_query->have_posts()) :
    	while ($wp_query->have_posts()) : $wp_query->the_post();
    
    		$taxonomies = get_terms( array(
    			'taxonomy' => 'floor4',
    			'hide_empty' => false
    		) );
    
    		if ( !empty($taxonomies) ) :
    
    			foreach( $taxonomies as $category ) {
    				$hover= get_field('hover', 'term_' .$category->term_id  );
    				echo $hover;
    			}
    
    		endif;
    
    	endwhile;
    endif; wp_reset_query(); 
  • I’m sorry, found my mistake: the field I called was in a group field.
    All works. Thank you for support.

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

You must be logged in to reply to this topic.