Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php HTML不显示在IF语句中(Wordpress)_Php_Wordpress_If Statement - Fatal编程技术网

Php HTML不显示在IF语句中(Wordpress)

Php HTML不显示在IF语句中(Wordpress),php,wordpress,if-statement,Php,Wordpress,If Statement,我有以下问题。我正在使用Wordpress的高级自定义字段为帖子创建字幕字段。我想给这个副标题一些样式,但是IF语句中的HTML代码没有显示在页面上。美元字幕确实显示了这一点 <?php $subtitle = the_field('subtitle'); ?> <?php if(strlen(trim($subtitle)) > 0): ?> <div class="post-sub-title"><?php $subtitle; ?>

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

<?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; ?>

将_字段()更改为获取_字段()。库多向阿迪亚·维卡斯致敬

您应该回显/打印它(subtitle变量)


尝试使用以下代码:

<?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; ?>

还有一件事

_字段()
不是默认的WordPress函数

您正在使用的“任意”插件可能具有相应的功能:

获取字段()

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


谢谢。

好的,谢谢您的回复,但这并不能解决HTML未显示的问题。那么我猜在调用trim函数后,subtitle中的数据长度不大于零。或者您的代码中一定有其他地方的bug,而不是您提供的部分。编辑:尝试不使用if语句打印它,看看它是否可以打印。我尝试了你的建议。但它不起作用。这张照片仍然没有显示出来。只有变量$subtitle(其中包含一些纯文本)有此功能。更改\u field()以使\u field()修复此问题!非常感谢你!
<?php $subtitle = get_field('subtitle'); ?>
<?php if(!empty(trim($subtitle))): ?>
  <div class="post-sub-title"><?php $subtitle; ?></div>
<?php endif; ?>