Support

Account

Home Forums General Issues get image from category custom field

Solved

get image from category custom field

  • Hi. I have an image custom field in each category. On my parent category page I am then displaying the children of that category along with title, link, description and the custom field image. The problem I have is that an image does display for each child but it’s the same one rather than each child category’s image. Here’s the code I’m using in my parent (the image custom field is “cat_half_image”):

    `
    <?php
    $cat = get_query_var(‘cat’);
    $args = array(
    ‘child_of’ => $cat,
    ‘orderby’ => ‘name’,
    ‘order’ => ‘ASC’
    );
    $attachment_id = get_field(‘cat_half_image’, ‘category_’. the_category_ID( $echo ) .”);
    $size = “thumbnail”;
    $categories = get_categories($args);
    foreach($categories as $category) {
    echo ‘<div class=”cat-block g_6″>’;
    echo ‘<p>term_id ) . ‘” title=”‘ . sprintf( __( “View all posts in %s” ), $category->name ) . ‘” ‘ . ‘>’ . wp_get_attachment_image( $attachment_id, $size ).’ </p> ‘;
    echo ‘<h2>’. $category->name . ‘</h2>’;
    echo ‘<p>’. $category->description . ‘</p>’;
    echo ‘</div>’; }
    ?>

    Any help or ideas would be greatly appreciate as I’ve been failing with this all weekend!

  • Hi @greencode

    The code you use to populate $attachment_id is OUTSIDE the sub categories loop. Therefor, it will not be dynamic for each category.

    You need to move this within the loop.
    You may find that the_category_ID doesn’t work within the loop. You will need to use $category->term_id of something similar

    Thanks
    E

  • Hi Elliot,

    Really appreciate you taking the time to help out with this. What I’m slightly confused about is that the code I have above is working correctly with the exception of the custom image field i.e. the title, link and description are all pulling in from the sub category correctly so I’m not sure what else to do?

    Forgive me here, my coding knowledge is not amazing.

    Alan

  • Hi @greencode

    The category name is showing because you have written the code WITHIN the loop:
    $category->name

    Your ACF code doesn’t work because you have written the code OUTSIDE the loop.
    My previous comment already outlines this, so if your not able to understand how this effects your code, I highly recommend that you hire a WP developer to help out with your project.

    Thanks
    E

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

    I’m trying to do the same thing, but ONLY for the current parent category.
    Here the code displays all the elements for ALL categories.

    Do you know what do I have to adapt ?

    Thanks in advance for your help.

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

The topic ‘get image from category custom field’ is closed to new replies.