doudie2693 2014-11-26 04:41
浏览 34

php'if exists'语法有问题

I need to create an if statement for the nbsp (space).

<?php echo get_post_meta($post->ID, 'the_term', true); echo '&nbsp;'; ?>

This is at the front of a line:

<h2><?php echo get_post_meta($post->ID, 'the_term', true); echo '&nbsp;'; ?>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 '&nbsp;';
} 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?

  • 写回答

2条回答 默认 最新

  • duan19911992 2014-11-26 04:51
    关注

    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]."&nbsp;" : ""; ?>Example</h2>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗