【问题标题】:Wordpress showing a default image on single.phpWordpress 在 single.php 上显示默认图像
【发布时间】:2016-01-13 23:15:44
【问题描述】:

我希望有人能帮助我解决我遇到的问题...

我目前正在构建一个 Wordpress 网站,并且正在整理帖子部分。一切似乎都很好,当在帖子上使用特色图片时,它可以很好地显示在此处的缩略图中:http://5.10.105.45/~learningforsusta/index.php/education-for-sustainable-development/,然后很好地显示在单个帖子中:http://5.10.105.45/~learningforsusta/index.php/efsc-post/example-post-1/

但是,在没有指定特色图片的帖子上,它似乎显示的是默认图片...

我的问题是,我怎样才能摆脱默认图像,如果没有特色图像,我希望它不显示任何东西......

这是我用来完成所有操作的代码:

<div class="container">   
<div class="row">

  <div class="col-md-9">

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

      <div class="page-header">            

        <?php
          $thumbnail_id = get_post_thumbnail_id(); 
          $thumbnail_url = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail-size', true );
          $thumbnail_meta = get_post_meta( $thumbnail_id, '_wp_attachment_image_alt', true);           
        ?>

        <p class="featured-image"><img src="<?php echo $thumbnail_url[0]; ?>" alt="<?php echo $thumbnail_meta; ?>"></p>


        <p><em>
          By <?php the_author(); ?> 
          on <?php echo the_time('l, F jS, Y');?>
          in <?php the_category( ', ' ); ?>.
          <a href="<?php comments_link(); ?>"><?php comments_number(); ?></a>
        </em></p>
      </div>

      <?php the_content(); ?>

      <hr>

      <?php comments_template(); ?>

    <?php endwhile; else: ?>

      <div class="page-header">
        <h1>Oh no!</h1>
      </div>

      <p>No content is appearing for this page!</p>

    <?php endif; ?>


  </div>

  <?php get_sidebar( 'blog' ); ?>

</div>

这里是与缩略图相关的 functions.php 文件中的代码...

add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size(150, 120, true);

我确定我遗漏了一些简单的东西,但我尝试过改变一些东西,但我就是想不通!

任何帮助将不胜感激...

谢谢,

肖恩

【问题讨论】:

  • 有趣...您共享的代码中没有任何内容表明存在默认图像。您是否尝试过在主题的源代码中搜索图像名称?您可能在某处缺少负责此默认输出的缩略图过滤器。
  • 老实说,这是我的第一次经历!这就是为什么它没有意义......下面的人似乎有答案 - 我只是错过了 if 语句。这就是回复:)

标签: php wordpress


【解决方案1】:
    1234563 @filter 减少结果集)
  1. 确保您没有任何可以在幕后执行此操作的第三方插件,例如this 一个。

【讨论】:

    【解决方案2】:

    您可以尝试使用has_post_thumbnail()函数并使用the_post_thumbnail()显示

    <?php if ( has_post_thumbnail()): // Check if thumbnail exists ?> 
        <p class="featured-image">       
            <?php the_post_thumbnail('post-thumbnails'); ?>
        </p>
    <?php endif; ?>
    

    【讨论】:

      【解决方案3】:

      在输出前检查特色图片是否存在:

      <?php
        if( has_post_thumbnail() ):
            $thumbnail_id = get_post_thumbnail_id(); 
            $thumbnail_url = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail-size', true );
            $thumbnail_meta = get_post_meta( $thumbnail_id, '_wp_attachment_image_alt', true);?>
      
      <p class="featured-image"><img src="<?php echo $thumbnail_url[0]; ?>" alt="<?php echo $thumbnail_meta; ?>"></p>
      <?php endif;?>
      

      【讨论】:

      • 啊,太棒了!非常感谢 - 现在很有意义! :)
      • 我很高兴,不知道为什么有人反对我的回答:(
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-07
      • 2011-11-01
      • 1970-01-01
      • 2012-01-27
      • 2011-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多