Home Forums General Issues Custom image sizes on category pages


Custom image sizes on category pages

  • Hi,
    I have some difficulties getting custom size working on category page.

    Here is what I have now for displaying the custom image (file: archive.php):

    $category_id = get_queried_object_id();
    $cat_id = 'category_'.$category_id;
    $image = get_field('category_image', $cat_id);?>
    <img src="<?php  print_r($image);?>" alt="">

    Now, a custom size called “square-image-posts” has been defined in functions.php.

    Tried to follow Wglassbrook and Jonathan solution in this thread:
    but can’t figure out how to make it work for category pages.

    Using Jonathan code:

    $image = get_field('category_image');
    echo $image['url']; //This is the full image
    echo $image['sizes']['medium']; //This is the medium image
    echo $image['sizes']['thumbnail']; //This is the thumbnail image
    echo $image['sizes']['square-image-posts']; //This is a custom sized image
    <?php $image = get_field('category_image') ['id']; ?>
    <?php if($image): //dont output an empty image tag ?>
    <img src="<?php echo $image['sizes']['square-image-posts']; ?>" width="<?php echo $image['sizes']['square-image-posts-width']; ?>" height="<?php echo $image['sizes']['square-image-posts-height']; ?>" alt="<?php echo $image['caption']; ?>" />
    <?php endif; ?>

    ..but nothing is showing.

    Thanks in advance for any help!

  • what happens if you do this

    $cat = get_queried_object();
    $cat_id = 'term_'.$cat->term_id;
    $image = get_field('category_image', $cat_id);
    echo '<pre>'; print_r($image); echo '</pre>';
  • Hi John,
    if I use your code it prints the url of the custom image at full size like this:

    I’ll need the code to show the image at custom size with classic WordPress srcset.
    Is it possible by using ACF?

  • If all that is output is the URL, then you have ACF set to return the URL of the image and the code you are using will not work. If you want to use code that references the array then you need to edit the field in ACF and change the “Return Value” to “Image Array”

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

The topic ‘Custom image sizes on category pages’ is closed to new replies.