• I have created a custom field for my post categories with AFC called “cf_category_img” and I have uploaded images for each category. So how would I go about listing all my post categories on my home page with a custom field value? I would like the list like this:

    <ul>
    <li>
    VALUE OF A CUSTOM FIELD CALLED "cf_category_img"
    <a href="category-url">Name of Category</a>
    </li>
    </ul>

    Thanks in advance!

Viewing 4 replies - 1 through 4 (of 4 total)
  • <?php
    $categories = get_categories();
    echo '<ul>';
    foreach( $categories as $category ) { ?>
        <li><a href="<?php esc_url( get_category_link( $category->term_id ) ); ?>"><?php esc_html( $category->name ); ?></a>
        <p>Image Url: <?php the_field('cf_category_img', $category); ?></p>
    <?php
    }
    echo '</ul>';

    I haven’t run this code but I am sure that it will works.

    Thread Starter kazie

    (@kazie)

    Awesome! I’m able to get the custom field value of the image, however, the category url and the category name isn’t showing. Any idea what could be wrong?

    Hi @kazie,

    I forgot to echo the category name and category url. Please find the attached correct code.

    <?php
    $categories = get_categories();
    echo '<ul>';
    foreach( $categories as $category ) { ?>
        <li><a href="<?php echo esc_url( get_category_link( $category->term_id ) ); ?>"><?php echo esc_html( $category->name ); ?></a>
        <p>Image Url: <?php the_field('cf_category_img', $category); ?></p>
    <?php
    }
    echo '</ul>';
    Thread Starter kazie

    (@kazie)

    Fantastic! Thanks man =)

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘List all categories with custom field values?’ is closed to new replies.