在IF语句中没有显示HTML(Wordpress)

时间:2013-10-15 13:24:32

标签: php wordpress if-statement

我有以下问题。我正在使用Wordpress的高级自定义字段为帖子创建字幕字段。我喜欢给这个副标题一些样式,但我在IF语句中的HTML代码没有在页面上显示。 $ subtitle确实显示了。

<?php $subtitle = the_field('subtitle'); ?>
<?php if(strlen(trim($subtitle)) > 0): ?>
  <div class="post-sub-title"><?php $subtitle; ?></div>
<?php endif; ?>

我花了几个小时寻找类似的问题,但找不到任何解决方案。所以这可能是我的一个新手错误。

解决方案

<?php $subtitle = get_field('subtitle'); ?>
<?php if(strlen(trim($subtitle)) > 0): ?>
  <div class="post-sub-title"><?php $subtitle; ?></div>
<?php endif; ?>

将the_field()更改为get_field()。 Kuddo来到Aditya Vikas!

3 个答案:

答案 0 :(得分:3)

你应该回显/打印它(字幕变量)。

<div class="post-sub-title"><?php echo $subtitle; ?></div>

答案 1 :(得分:2)

尝试使用这段代码:

<?php if(strlen(trim($subtitle)) > 0): ?>
  <div class="post-sub-title"><?=$subtitle?></div>
<?php endif; ?>

而不是:

<?php if(strlen(trim($subtitle)) > 0): ?>
  <div class="post-sub-title"><?php $subtitle; ?></div>
<?php endif; ?>

还有一件事!

the_field()不是默认的WordPress功能

您正在使用的'what'插件可能具有相应的功能:

get_field()

答案 2 :(得分:1)

试试这个:

<?php $subtitle = get_field('subtitle'); ?>
<?php if(!empty(trim($subtitle))): ?>
  <div class="post-sub-title"><?php $subtitle; ?></div>
<?php endif; ?>

感谢。