Alan Carr
Alan Carr

Reputation: 322

Advanced Custom Fields Conditional Statement on Tag Page

I have Advanced Custom Fields installed and have added a custom field on the edit tag page, it is an for an image to show for different tags. I'm trying to create a conditional statement to work like this in archive.php:

if tag image show image else show something else

The code i'm using just doesnt show anything and I can't understand what I'm doing wrong, could somebody help please.

<?php
global $post;
while ( have_posts() ) : the_post();
$tag_image = get_field('tag_image',$post->ID);
if($tag_image['url'] != '')
{ ?>
<img src="<?php echo $tag_image['url']; ?>" />
<?php echo tag_description(); ?>
<?php } else { ?>
<?php echo tag_description(); ?>
<?php } ?>
<?php endwhile; ?>

Upvotes: 0

Views: 444

Answers (3)

Manohar singh
Manohar singh

Reputation: 509

I think Your loop not getting ID's properly Try the below code

global $post;
while ( have_posts() ) : the_post();
$tag_image = get_field('tag_image',$post->ID);
if($tag_image['url'] != '')
{                   
echo $tag_image['url'];
}
endwhile;

Hope this Helps.

Upvotes: 0

Manohar singh
Manohar singh

Reputation: 509

You have to use $tag_image['url'] inorder to get image source

$tag_image = get_field('tag_image',$post->ID);          

if($tag_image['url'] != '')
{ ?>
<img src="<?php echo $tag_image['url']; ?>"/>
<?php
}else{
  Do some thing
 }

Hope this helps you

Upvotes: 1

DShults
DShults

Reputation: 389

Is tag_image the path to the image? Try a simple <?php echo ('tag_image')?> to make sure the data is there...?

Upvotes: 0

Related Questions