Home Forums General Issues List categories with extra fields


List categories with extra fields

  • Hi,
    I’m trying to display all post categories on a custom page template.

    List all category names, with a custom field called sub_title.

    $args=array( 'hide_empty' => '0');
      foreach($categories as $category) {
    	  echo '<li>'  . $category->name . '</li>';
              global $post;
              $terms = get_the_terms($post->ID, 'category');
              if( !empty($terms) )
              $term = array_pop($terms);
              $custom_field = get_field('sub_title', 'category_' . $term->term_id );
              echo '<li>'  . $custom_field . '</li>';

    Any help would be much appreciated,

    Great plugin by the way!

  • Hi Tom!

    What you got there is a little strange…

    The logic you have says:
    Get all regular categories, loop through them and in each category fetch the current posts categories then get just the last category object and fetch the custom field of that category.

    What exactly are you trying to do? Do you want to display the current posts categories or do you want to display all categories as links(or without links)?

  • Hi Jonathan,

    I’m trying to display all categories on a custom page template, like so

    Category Image (custom field)
    Category Title
    Category Sub Title (custom field)
    Category Description

    And repeat for every category.

    Any help would be greatly appreciated 🙂

  • Alrightie then.. your code is extremely overdoing it!

    You’ll want something like this:

    $args = array( 'hide_empty' => '0');
    $categories = get_categories($args);
    	echo '<ul>';
    	foreach($categories as $category) {
    		echo '<li>';
    		$image = get_field('nameofimagefield', 'category_'.$category->term_id);
    		echo '<img src="' . $image . ' />'; //change depending on the return value of the image field
    		echo '<span class="cat-title">' . $category->name . '</span>';
    		echo '<span class="cat-subtitle">' . get_field('nameofsubtitlefield', 'category_'.$category->term_id) . '</span>';
    		echo '<span class="cat-description">' . $category->description . '</span>';
    		echo '</li>';
    	echo '</ul>';

    dont forget to change the fieldnames

  • Thanks alot Jonathan, I’m having an problem displaying the image with your code, its returning <img src="Array" />?

    Thanks for your help again, I’m still learning 🙂

  • if you change the return of your image field (in admin) to url it’ll work.. 🙂

  • Sorted, Thanks alot Jonathan!

  • Hmm
    Looks nice but it doesnt work for me at all..
    Im using ACF Pro.
    Can some one give me a hint please.


  • Hi @dezerter

    Please start your own topic with whatever issue you have instead and I’ll be sure to help you there 🙂

  • Actually I got it working now 🙂

    Thanks man !

  • In case any one else finds this and is having issues, there is a missing quotation mark in this line:

    echo '<img src="' . $image . ' />'; //change depending on the return value of the image field

    should be

    echo '<img src="' . $image . '" />'; //change depending on the return value of the image field

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

The topic ‘List categories with extra fields’ is closed to new replies.