我有以下问题。我正在使用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()。对阿迪亚·维卡斯的赞美!
你应该回显/打印它(字幕变量)。
<div class="post-sub-title"><?php echo $subtitle; ?></div>
尝试使用这段代码:
<?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功能
您使用的"whatever"插件可能具有相应的功能:
get_field()
试试这个:
<?php $subtitle = get_field('subtitle'); ?>
<?php if(!empty(trim($subtitle))): ?>
<div class="post-sub-title"><?php $subtitle; ?></div>
<?php endif; ?>
谢谢。