A
A
aliasst2018-06-28 11:28:03
WordPress
aliasst, 2018-06-28 11:28:03

How to correctly check the presence of an arbitrary field for block output in a VI?

Tell me, who knows .. If there is an arbitrary field, it is necessary to display a block with text ... where the text is taken from this arbitrary field .. In this example, an arbitrary field named text1 ... But this construction does not work ... Tell me where the error is Or maybe I'm doing something completely wrong?

<?php if (get_post_meta($post->ID, 'text1', true) !== '') {?>
  <div class="text">
      <?php echo (get_post_meta($post->ID, 'text1', true)); ?></div>
      <?php}?>

You can, of course, do that..
<?php if (get_post_meta($post->ID, 'text1', true) !== '') 
{echo "<div class="text">".get_post_meta($post->ID, 'text1', true)."</div>"; }?>

But the first option is important to me.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
eremenkoEvgeniy, 2018-06-28
@aliasst

Like this:

if (isset(get_post_meta($post->ID, 'text1', true))) {
//your code
}

or simply:
if (get_post_meta($post->ID, 'text1', true)) {
//your code
}

or even more concisely:
if ($text = get_post_meta($post->ID, 'text1', true)) {
echo '<div class="text">'.$text.'</div>';
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question