Support

Account

Home Forums ACF PRO Getting ACF field from a taxonomy in a page with WP_query

Solved

Getting ACF field from a taxonomy in a page with WP_query

  • Hi there,

    I’m stuck with this. I’m trying to get the ACF field image ‘logo_marque’ that I used for the taxonomy ‘marques-produit’ from my CPT ‘produits’ on my front page.

    I’m using this code :

    					<?php 
    						$logo = get_field('logo_marque');
    						$args = array( 
    						'post_type' => 'produits', 
    						'tax_query' => array(
    							array (
    								'taxonomy' => 'marques-produits',
    								'fields' => $logo,
    							)
    						),
    						'posts_per_page' => -1, 
    						'order' => 'DESC' );
    						$loop = new WP_Query( $args );
    						while ( $loop->have_posts() ) : $loop->the_post(); ?>
    						<div class="product">
    
    						<img src="<?php echo esc_url($logo['url']); ?>" alt="<?php echo esc_attr($logo['alt']); ?>" />
    						</div>
    						<?php endwhile; ?>
    					<?php wp_reset_query(); ?>    
    

    Thank you for your help.

  • Sorry, but your question and your code do not make any sense and I’m not sure where to begin. Can you give a better explanation of what you are trying to do?

  • I created a taxonomy ‘marques-produit’ for my CPT ‘produits’. I used ACF to add the field image ‘logo_marque’ on my taxonomy.
    Now I want to display on my front page the field ‘logo_marque’ for every label like this
    <img src="<?php echo esc_url($logo['url']); ?>" alt="<?php echo esc_attr($logo['alt']); ?>" />
    Thank you

  • That really does not help?

    I see a query for posts. This has a taxonomy query. This includes 'fields' => $logo,. There is not “fields” argument for a taxonomy query. There is a “field” argument, but that argument does not take the value you are attempting to use. So I can’t really tell what posts you are trying to get here?

    From what I understand you are also trying to get an image file from the term here $logo = get_field('logo_marque'); Getting a value from a term requires that you know the term, or at least the term ID and supply this when getting the ACF field. https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/

    I a guessing that you want to show posts in a term and show the term images for the post. In this case your taxonomy query requires the term ID, name, or slug to search posts by. For example

    
    'tax_query => array(
      array(
        'taxonomy' => 'marques-produits',
        'field' => 'term_id',
        'terms' => $term_id
      )
    )
    
  • I finally found out the solution. I was wrong from the start. This code works for me, it can probably be optimized

    <?php
    	$args = array(
    		'taxonomy' => 'marques-produits',
    	    'orderby' => 'name',
    	    'order'   => 'ASC'
    	    );	
    	   $cats = get_categories($args);
    	   foreach($cats as $cat) {
    		   
    		   if ($logo = get_field('logo_marque','marques-produits_'.$cat->term_id)) {
    			   $cats_keep[] = $cat;
    			   }
    			}
    			foreach($cats_keep as $cat) {
    		   
    	?>
    	      <a href="<?php echo get_category_link( $cat->term_id ) ?>">
    	           <img src="<?php echo esc_url($logo['url']); ?>" alt="<?php echo esc_attr($logo['alt']); ?>" />
    	      </a>
    	<?php
    	   }
    	?>
    
  • Edit the right code :

    					<?php $args = array(
    						'taxonomy' => 'marques-produits',
    					    'orderby' => 'name',
    					    'order'   => 'ASC'
    					    );	
    					$cats = get_categories($args);
    					foreach($cats as $cat) {
    						$logo = get_field('logo_marque', 'marques-produits_'. $cat->term_id .'');
    						echo 
    						'<a href="' . get_category_link( $cat->term_id ) . '" title="' . sprintf( __( "Voir tout les produits %s" ), $cat->name ) . '">
    							<img src="' . $logo['url']; ?>" alt="<?php echo $logo['alt'] . '">
    						</a>';
    						}
    					?>
    
Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.