0

I want to print a block of HTML only when get_field('cennik_title') has value assigned to it inside the loop, when there is no value inserted inside the custom field, the block should be skipped, but in this code the HTML block is printing even I did not insert value for 'cennik_title' custom field, I did use unset(), but its not working, any other way I can achieve it?

<div class="card-body ouroffer">

<?php 
$args = array( 'post_type' => 'cennik_post',
'category_name' => 'Stylizacja Rzęs i Paznokci',
'orderby' => 'date',
'order'   => 'ASC',);
$cennik_query = new WP_Query ($args);
$i=1;
if(have_posts()) : while ( $cennik_query->have_posts() ) : $cennik_query -> the_post();
?>

    <?php $ct = get_field('cennik_title');
    if (isset($ct)):
    ?>

    <ol>
        <li><?php the_field('cennik_title'); ?></li>
        <li>
            <ul>

                <?php
                endif; 
                ?>

                <li><a href="<?php echo get_post_permalink() ?>"><span><?php echo   $i . ". " ?><?php the_title(); ?></span></a><span><?php the_field('cena'); ?> zł / <?php the_field('trwania'); ?> min</span></li>

                <?php
                if (isset($ct)):
                ?>

            </ul>
        </li>
    </ol>

    <?php
    endif; 
    ?>

<?php
$i++;
unset($ct);
endwhile; endif;
?>
</div>
0

isset() is the wrong function to use here. $ct is set because you're setting it here:

$ct = get_field('cennik_title')

isset() will only be false if $ct was never defined or is set to NULL. get_field() will return false or an empty string if there's no value and isset() will return true for those.

You want to check if it has a value but is not false or empty. The simplest way to do that is just:

if ( $ct ) :

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.