Support

Account

Home Forums Front-end Issues Can't seem to properly retrieve ACF of a category term

Solving

Can't seem to properly retrieve ACF of a category term

  • I’m using the following code in my single.php:

    $category = get_the_category();
    	$cat = 'category_'.$category[0]->term_id;
    	
    	if (get_field('banner_ad')){			
    		echo '<div class="" style="margin:30px auto 0; width: 100%;text-align:center;"><img src="' . get_field('banner_ad') . '" /></div>';
    	} elseif (get_field('banner_ad', $cat)) {
    		echo '<div style="margin:30px auto 0; width: 100%;text-align:center;"><img src="' . get_field('banner_ad', $cat) . '" /></div>';
    	} else {
    		echo '<div class="' . $cat . '" style="margin:30px auto 0; width: 100%;text-align:center;"><img src="' . get_field('banner_ad', 'option') . '" /></div>';
    }

    What I have set up is an ACF for an image upload that is available in an Options page, in the Categories, and on individual posts. The idea is that the if then statement would check to see if there was an image uploaded at the post level, if not, then get the image from the category, if there wasn’t one, then get the image set in the options page.

    It’s only 66% working. I cannot get it to display the image from the category…for whatever reason, when it does load that part, it appends CSS to the image element:

    width="0" height="0" style="display: none !important; visibility: hidden !important; opacity: 0 !important; background-position: 0px 0px;"

    I have no idea why it only does it to the category image.

  • This reply has been marked as private.
  • For what it’s worth, I tried updating the ACF for this particular image, and since then, it’s removed itself from the category.

    Here’s what it looks like in a Category Term:
    Screenshot of Category

    When adding a new category term, I have the option to upload an image, but once saved, the image options disappear.

  • Hi @nwtd

    I’ve just checked your website and the banner looks great on my end. Regarding the image options issue, could you please try to reproduce the issue on one of the WordPress’ stock themes (like Twenty Fifteen) with other plugins deactivated? If it disappears, then you can activate the theme and plugins one by one to see which one causes the issue.

    If the issue persists, could you please share the JSON export of your field group so I can test it out on my installation?

    Thanks!

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

The topic ‘Can't seem to properly retrieve ACF of a category term’ is closed to new replies.