Am struggling to get the below to work.

I just want to display the get_post_meta 'title_override_text' if it contains any value otherwise just display the standard title (already catered for in functions.php).

<?php $h1override = get_post_meta($id, "title_override_text", true); ?>
<?php if (!empty($h1override)) { ?>
    <h1><span><?php title_text(); ?></span></h1>
<?php } else { ?>
    <h1><span><?php title_override_text(); ?></span></h1>
<?php } ?>

It correctly displays 'title_override_text' if there is a value but when the field is empty, it doesn't display anything at all (i.e not 'title_text').

How can I display 'title_override_text' if it contains any value (it is a text field)?

Is there a better way of doing this like using (for example):-

if(get_post_meta($id, "page_title_override_text", true) == ""
  • 1
    Shurely it should be if (empty($h1override)) rather than if (!empty...? – bonger Dec 31 '14 at 15:05
  • @bonger I tried that as well but it had no effect. I actually managed to achieve what I was trying to do a better way - will post it as an answer as soon as I can... – zigojacko Jan 2 '15 at 9:15

I actually managed to achieve what I needed to do in the end by using the below:-

<?php if(get_post_meta($id, "h1_title_override", true) !== '') { ?>
    <h1><?php title_override_text(); ?></h1>
<?php } else { ?>
    <h1><?php title_text(); ?></h1>
<?php } ?>

With the necessary functions in place of course...

| improve this answer | |

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.