WordPress the_content() 不显示图像(如果周围没有其他内容)

WordPress the_content() does not show images (if not other content surrounding)

我为 WordPress(4.4.2) 构建了一个自定义主题。我使用标准 built-in 编辑器来创建帖子。如果我创建一个 Post,例如,一个标题和一个段落以及一张图片,就会显示内容(图片也是如此)。但是,如果我只输入标题,然后在编辑器的内容区域中只留下图像而没有进一步的文字,它就不会显示在网站上。

<?php if (have_posts()) : while (have_posts()) : the_post();?>
  <div class="container-fluid site-content content-area" role="main">
    <div class="container">
      <?php if (!empty_content($post->post_content)) :?>
      <div class="content row col-md-10 xcentered">

        <?php the_content(); ?>
      </div>
    <?php endif; ?>
    </div>
  </div>
<?php endwhile; endif; ?>

我也尝试了没有 if 语句的代码,但是 the_content() 没有显示没有任何其他内容的图像。

除 div.container 之外的所有内容都已呈现。只要我添加一个字符 before/after,图像标题、字符和图像就会被渲染。

有什么地方(或)我必须做出改变的建议吗?为什么无法识别图像 "standalone"?

您正在使用的函数 'empty_content',您已在问题的评论中提供:

function empty_content($str) { 
    return trim(str_replace('&nbsp;','',strip_tags($str))) == '';
}

当我将 <img src="someimg.png"> 放入此函数时,它 returns 为真,这意味着您的 <div> 未呈现。

这正是您所描述的行为。

尝试http://phpfiddle.org/以下代码:

function empty_content($str) { 
    return trim(str_replace('&nbsp;','',strip_tags($str))) == '';
}

$only_img = '<img src="someimg.png">';
$img_and_more = '<img src="someimg.png"><p>Some other stuff</p>';



var_dump(empty_content($only_img));
var_dump(empty_content($img_and_more));

您会看到 $only_img 变量转储为 true$img_and_more 转储为 false。