Home › Forums › General Issues › get image from category custom field › Reply To: get image from category custom field
Thanks so much for drumming this into me. I’ve finally figured it out. You were correct on both accounts i.e. needed to be inside the loop and also needed to use
$category->term_id
So the final code I have is
<?php
$cat = get_query_var('cat');
$args = array(
'child_of' => $cat,
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($args);
foreach($categories as $category) {
$attachment_id = get_field('cat_half_image', 'category_'. $category->term_id .'');
$size = "half-img";
echo '<div class="cat-block g_6">';
echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . wp_get_attachment_image( $attachment_id, $size );
echo '<h2>'. $category->name . '</h2>';
echo '</a>';
echo '<p>'. $category->description . '</p>';
echo '</div>'; }
?>
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We’re reaching out to our multilingual users to ask for help in translating ACF 6.1. Help make sure the latest features are available in your language here: https://t.co/TkEc2Exd6U
— Advanced Custom Fields (@wp_acf) May 22, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.