drmlxgmqn18198265 2016-05-24 21:14
浏览 235
已采纳

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

I build a custom theme for WordPress(4.4.2). I uses the standard built-in Editor to create posts. If I create a Post with, say, a headline and a paragraph AND an image, the content is shown (so is the image). But If I just type in a title and in the content area of the editor I leave JUST the image with no further text, it will not be shown on the website.

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

I also tried the code without the if statement, but the_content() does not display an image without any other content.

Everything but the div.container is rendered. And as soon as I add just a single character before/after the image title, character AND image are rendered.

Any suggestions where (else) I have to make changes? Why is an image not recognized "standalone"?

  • 写回答

1条回答 默认 最新

  • dro60731 2016-05-24 22:08
    关注

    The function 'empty_content' you are using, which you have provided in the comments on your question:

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

    When I put <img src="someimg.png"> into this function, it returns true, meaning your <div> is not rendered.

    This is exactly the behavior you are describing.

    Try to http://phpfiddle.org/ the following code:

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

    You will see that the $only_img variable dumps to true and the $img_and_more dumps to false.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥500 寻找一名机械工程师完成pcb主板设计(拒绝AI生成式答案)
  • ¥15 关于#mysql#的问题:UNION ALL(相关搜索:sql语句)
  • ¥15 matlab二位可视化能否针对不同数值范围分开分级?
  • ¥15 已经创建了模拟器但是不能用来运行app 怎么办😭自己搞两天了
  • ¥15 关于#极限编程#的问题,请各位专家解答!
  • ¥20 win11账户锁定时间设为0无法登录
  • ¥45 C#学生成绩管理系统
  • ¥30 matlab appdesigner私有函数嵌套整合
  • ¥15 vb6.0使用jmail接收smtp邮件并另存附件到D盘
  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标