Support

Account

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>'; }
    ?>