I use Advanced Custom Fields plugin in my WordPress theme, to use custom fields in the post area.
I want to change the class of a <span>
depending on the status of a field, but it isn't working as expected. For instance, when the status is set to new
, I want <span class="status">
, but with the following code this isn't happening:
<?php
echo "<span ";
if(the_field('status') == "new") {
echo "class=\"status\"";
} else {
echo "class=\"statusSold\"";
}
echo ">";
the_field('status');
echo "</span>";
?>
This is the whole script:
<?php
$args = array(
'posts_per_page' => 9,
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()):
while ($the_query->have_posts()) : $the_query->the_post();
?>
<li>
<a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>">
<span class="hover"> </span>
<?php
$class = the_field('status') == 'new' ? 'status' : 'statusSold';
echo '<span class="' . $class . '">' . the_field('status') . '</span>';
?>
<?php if ( has_post_thumbnail()) the_post_thumbnail('post-thumbnails'); ?>
<span class="title"><?php the_title(); ?></span>
<span class="type"><?php foreach((get_the_category()) as $category) {echo $category->cat_name; } ?></span>
<span class="address"><?php the_field('address'); ?></span>
<span class="prize"><?php the_field('price'); ?></span>
</a>
</li>
<?php
endwhile;
endif;
?>
What must I do to make this work as expected?