Support

Account

Home Forums Front-end Issues Displaying fields from a taxonomy term on a page other than 'category.php'.

Solved

Displaying fields from a taxonomy term on a page other than 'category.php'.

  • Hello guys!

    I would like to list all my categories on a page called “Portfolio” and NOT on the ‘category.php’ page.

    This listing must have an image and the name of the category.
    The image in the WordPress category has been created according to the following link: https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/

    The code used to list the categories is this below, but I do not know what to do in order for the code to take the “image_category” field created in the ACF for the categories.

    CODE

    
    <ul class="list_cat">
      <?php
      $args = array(
        'type'                     => 'post',
        'child_of'                 => 2,
        'parent'                   => '',
        'orderby'                  => 'name',
        'order'                    => 'ASC',
        'hide_empty'               => 1,
        'hierarchical'             => 1,
        'exclude'                  => '2',
        'include'                  => '',
        'number'                   => '',
        'taxonomy'                 => 'category',
        'pad_counts'               => false
      );
    
      $cats = get_categories( $args );
      foreach( $cats as $cat ){
        echo '<li><img class="img_cat" src="IMAGE_HERE" alt=""><a href="' . get_category_link( $cat->term_id ) . '">' . $cat->name . '</a></li>';
      }
      ?>
    </ul>
    

    Can anyone give me a light?

  • 
    src="<?php the_field('image_field', $cat); ?>"
    
  • John Huebner, your code returned this to me:

    <li><img class="img_cat" src="124, , casamentos, , , image/jpeg, http://localhost/jd/wp-content/uploads/2018/03/casamentos.jpg, 1280, 486, Array" alt=""><a href="localhost/jd/category/casamentos">Casamentos</a></li>

    The data is in an array, how to just get the URL?
    Thank you for your help.

  • You are returning an image object/array. The code I gave was just an example to show supplying the term to ACF in order to get the correct values. There is an article here on the different ways to use the values returned by ACF for image fields https://www.advancedcustomfields.com/resources/image/

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

The topic ‘Displaying fields from a taxonomy term on a page other than 'category.php'.’ is closed to new replies.