drjun1994 2014-07-29 07:37
浏览 47
已采纳

php if语句我需要根据自定义字段给出的字符串回显类[关闭]

I use Advanced Custom Fields plugin in my WordPress theme, to use custom fields in the post area.

I want to change the class of a <span> depending on the status of a field, but it isn't working as expected. For instance, when the status is set to new, I want <span class="status">, but with the following code this isn't happening:

<?php
echo "<span ";

if(the_field('status') == "new") {
  echo "class=\"status\"";
} else {
  echo "class=\"statusSold\"";
}

echo ">";
the_field('status');
echo "</span>";
?>

This is the whole script:

<?php
  $args = array(
    'posts_per_page' => 9,
  );
  $the_query = new WP_Query($args);

  if ($the_query->have_posts()):
    while ($the_query->have_posts()) : $the_query->the_post();
?>

<li>
  <a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>">
    <span class="hover">&nbsp;</span>
    <?php
      $class = the_field('status') == 'new' ? 'status' : 'statusSold';
      echo '<span class="' . $class . '">' . the_field('status') . '</span>';
    ?>
    <?php if ( has_post_thumbnail()) the_post_thumbnail('post-thumbnails'); ?>
    <span class="title"><?php the_title(); ?></span>
    <span class="type"><?php foreach((get_the_category()) as $category) {echo $category->cat_name; } ?></span>
    <span class="address"><?php the_field('address'); ?></span>
    <span class="prize"><?php the_field('price'); ?></span>
  </a>
</li>

<?php
    endwhile;
  endif;
?>

What must I do to make this work as expected?

  • 写回答

2条回答 默认 最新

  • draj30958 2014-07-29 07:47
    关注

    I can't really tell what is wrong with your current attempt - but it is messy. Try this instead:

    $class = the_field('status') == 'new' ? 'status' : 'statusSold';
    
    echo '<span class="' . $class . '">' . the_field('status') . '</span>';
    

    This should do what you want it to do. Check out this demo. If this doesn't work, then the problem is something else - most likely to do with the_field('status'). If more debugging is necessary. Try simply echoing the_field('status') to confirm that it is returning the appropriate response.


    Update

    The problem is that your function, the_field(), does not return a value, it simply echos it. Consider this demo to mimic your problem. I'd say the best way to fix this would be to overload the function to return a value. However, Here is a pretty hacky fix that should work for you (Edit: This is probably not the best approach, See update 2 and @LeonardChallis's answer. That is probably the solution you need):

    echo '<span style="display:none">';
    $class = strcmp(the_field('status'), 'new') ? 'status' : 'statusSold';
    echo '</span>';
    
    echo '<span class="' . $class . '">';
    the_field('status');
    echo '</span>';
    

    Update 2

    Or, better yet, use the appropriate function. According to @LeonardChallis, that function is get_field().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥15 pip install后修改模块路径,import失败,需要在哪里修改环境变量?
  • ¥15 爬取1-112页所有帖子的标题但是12页后要登录后才能 我使用selenium模拟登录 账号密码输入后 会报错 不知道怎么弄了
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline