Support

Account

Home Forums General Issues Taxonomy Term Image Field for Product Categories

Solving

Taxonomy Term Image Field for Product Categories

  • I have a custom post type of products which has a taxonomy of product categories.

    I have added an ACF image field to this.

    I want to be able to output a title, description and image for each category on archive-products.php (which is my template for /products/)

    I have been using a get_categories query as below for the standard output, I have tried to include the_field for the image but I cannot get it to display, how can I do it? I’ve read the documentation but can’t get it going.

    <?php
    $args = array(
      'orderby'		=> 'name',
      'order'		=> 'ASC'
      );
    $categories = get_categories($args);
      foreach($categories as $category) { 
        echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
        echo '<p> Description:'. $category->description . '</p>';
        echo '<p> Post Count: '. $category->count . '</p>'; 
        
    } 
    ?>
  • Hi @maxis,

    Well, to get ACF values associated with a particular term, you have to pass an extra argument to the_field(...) or get_field(...) method to target the term ID. This argument can either be a term object or a term string as explained in the docs. The following code should work for you:

    foreach($categories as $category):
    	...
    	$image = get_image('image_field_name', $category);
    
    	echo '<img src="'.$image['url'].'" alt="'.$image['alt'].'" />';
    
    endforeach;
  • hi i need to get the category

      image

    and title from the tasxonomy term

    plz help me to find the solution

    here is my code

    <?php

    $terms = get_field(‘taxonomy_field_name’);

    if( $terms ): ?>

    <?php endif; ?>

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

The topic ‘Taxonomy Term Image Field for Product Categories’ is closed to new replies.