【问题标题】:WordPress feature image using if statement inside the img src attributeWordPress 特征图像在 img src 属性中使用 if 语句
【发布时间】:2017-09-06 12:17:34
【问题描述】:

我正在使用 if has_post_thumb 函数访问 WordPress 中自定义帖子类型的特征图像。一切都很好,除了 img 标签中的尾随 >" 实际上显示在页面上。我只是写错了吗?我必须将 php 保留在 src 属性中,因为 img 标签中有一些重要的 Bootstrap 类。请参阅代码如下:

                    <!-- create new loop and access custom post type for services using wordpress fxn -->
                    <!-- create var called loop and store a WP array for custom post type (CPT) services offered, ordered by id and ascending -->
                    <?php $loop = new WP_Query( array( 'post_type' => 'accolades', 'orderby' => 'post_id', 'order' => 'ASC') ); ?>

                    <!-- check to see if loop has posts and access posts from CPT. Same for all CPT -->
                    <?php while( $loop->have_posts() ) : $loop->the_post(); ?>

                        <!-- no acf used below it is all native WP using CPT -->
                        <div class="col-sm-4">
                            <div class="row">
                                <div class="col-sm-3">
                                    <!-- check if there is a post thumbnail img or feature image. this grabs the feature img if there is one below. add an else statement to the if to display image if none uploaded -->
                                    <img class="img-responsive img-circle" src="<?php if( has_post_thumbnail() ){ the_post_thumbnail(); } ?>">
                                </div><!-- /.col-sm-3 -->
                                <div class="col-sm-9">
                                    <blockquote>
                                    <!-- CPT content in content editor -->
                                        <p><?php the_content(); ?></p>
                                        <!-- CPT title -->
                                        <small><?php the_title(); ?></small>
                                    </blockquote>
                                </div><!-- /.col-sm-9 -->
                            </div><!-- /.row -->
                        </div><!-- /.col-sm-4 -->

                    <!-- close the while loop -->
                    <?php endwhile; ?>

【问题讨论】:

  • &lt;?php echo ((has_post_thumbnalil()) ? the_post_thumbnail() : '' )?&gt;
  • the_post_thumbnail 显示图像 - 我认为您需要使用

标签: php html wordpress twitter-bootstrap


【解决方案1】:

你可以这样得到:

 <?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 
 'thumbnail' ); ?>
  <img src="<?php echo $url ?>" />

【讨论】:

    【解决方案2】:

    the_post_thumbnail() 函数将返回图像而不是 url。请改用 the_post_thumbnail_url()。

    请参考,https://developer.wordpress.org/reference/functions/the_post_thumbnail/ https://codex.wordpress.org/Function_Reference/the_post_thumbnail_url

    【讨论】:

    • 成功了!网页上不再显示尾随“>”。谢谢
    • @GingaRanga,我很高兴它成功了。如果您认为它解决了您的问题,请接受答案。
    • 抱歉,我的声望低于 15,所以我的赞成票会被计算在内,但不会公开显示。我已经投票了。谢谢
    • @GingaRanga,嘿,别担心。这不是赞成,而是接受答案。此外,它不仅适用于这个问题。对于您在 SO 中发布的所有问题,如果您认为任何答案都解决了您的问题,您始终可以通过单击左侧的灰色勾号来接受答案。请参考,stackoverflow.com/help/someone-answers
    【解决方案3】:

    试试这样:

    if(has_post_thumbnail()){
      echo "<img class='img-responsive img-circle' src='your image'>"
    }
    else{
       echo "<img class='img-responsive img-circle' src='your image'>"
    }
    

    <?php echo ((has_post_thumbnalil()) ? the_post_thumbnail() : '' )?> 
    

    【讨论】:

      猜你喜欢
      • 2015-04-24
      • 1970-01-01
      • 2015-12-14
      • 2021-03-30
      • 2016-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-17
      相关资源
      最近更新 更多