[英]WordPress feature image using if statement inside the img src attribute
I am accessing the feature image of a custom post type in WordPress by using an if has_post_thumb function. 我正在使用if has_post_thumb函数访问WordPress中自定义帖子类型的功能图像。 Everything works great except the trailing >" in the img tag is actually displaying on the page. Am I just writing it wrong? I have to keep the php inside the src attribute because there is some important Bootstrap classes inside the img tag. Please see code below:
除了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; ?>
The the_post_thumbnail() function will already return the image not the url. the_post_thumbnail()函数将已经返回图像而不是URL。 Use the_post_thumbnail_url() instead.
请改用the_post_thumbnail_url()。
Please refer, https://developer.wordpress.org/reference/functions/the_post_thumbnail/ https://codex.wordpress.org/Function_Reference/the_post_thumbnail_url 请参阅https://developer.wordpress.org/reference/functions/the_post_thumbnail/ https://codex.wordpress.org/Function_Reference/the_post_thumbnail_url
Try Like this : 像这样尝试:
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'>"
}
OR 要么
<?php echo ((has_post_thumbnalil()) ? the_post_thumbnail() : '' )?>
You can get it like this: 您可以这样获得:
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID),
'thumbnail' ); ?>
<img src="<?php echo $url ?>" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.