Reputation: 9
I need to create an if statement for the nbsp (space).
<?php echo get_post_meta($post->ID, 'the_term', true); echo ' '; ?>
This is at the front of a line:
<h2><?php echo get_post_meta($post->ID, 'the_term', true); echo ' '; ?>Example
If the term exists, then it'll show 'Term Example'. If not, I need to show 'Example'. But right now, without an if, I get ' Example'. Note the ugly space.
I think I'd done this before, using this:
<?php if (post_custom('the_term')) {
echo get_post_meta($post->ID, 'the_term', true); echo ' ';
} else {
echo '';
?>
... but it's not working. Server error.
I also tried this:
<?php $the_term_check = get_post_meta($post->ID, 'the_term', true);
if (!empty($the_term_check))
echo '<h2>'; echo get_post_meta($post->ID, 'the_term', true); echo ' Example'</h2>';
if (empty($the_term_check))
echo'<h2>Example</h2>';
?>
Still not it. This one echos 'Example' twice -- and one of them doesn't even have the right HTML markup.
Either plain PHP or WordPress-specific code would work, as this is pulling the custom meta from a custom post type -- if it exists.
Any ideas?
Upvotes: 0
Views: 74
Reputation: 74219
You are missing a closing brace for else { echo '';
Then this echo ' Example'</h2>'
there is one quote too many after "Example".
Rewrites
<?php if (post_custom('the_term')) {
echo get_post_meta($post->ID, 'the_term', true); echo ' ';
} else {
echo '';
} // added missing brace
?>
and
<?php $the_term_check = get_post_meta($post->ID, 'the_term', true);
if (!empty($the_term_check))
echo '<h2>'; echo get_post_meta($post->ID, 'the_term', true); echo ' Example</h2>';
if (empty($the_term_check))
echo'<h2>Example</h2>';
?>
or as, and with added braces (it's good practice)
<?php
$the_term_check = get_post_meta($post->ID, 'the_term', true);
if (!empty($the_term_check)){
echo '<h2>'; echo get_post_meta($post->ID, 'the_term', true);
echo ' Example</h2>';
}
if (empty($the_term_check)){
echo'<h2>Example</h2>';
}
?>
Upvotes: 1
Reputation: 2330
I think this would work for you.
<h2><?php echo !empty(get_post_meta($post->ID, 'the_term', true)[0]) ? get_post_meta($post->ID, 'the_term', true)[0]." " : ""; ?>Example</h2>
Upvotes: 0