0
votes

I am accessing the feature image of a custom post type in WordPress by using an if has_post_thumb function. 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:

                    <!-- 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; ?>
3
<?php echo ((has_post_thumbnalil()) ? the_post_thumbnail() : '' )?>line88
the_post_thumbnail displays an image - I think you need to use <?php the_post_thumbnail_url( 'you image size here' ); ?>Stender

3 Answers

1
votes

The the_post_thumbnail() function will already return the image not the url. Use the_post_thumbnail_url() instead.

Please refer,https://developer.wordpress.org/reference/functions/the_post_thumbnail/ https://codex.wordpress.org/Function_Reference/the_post_thumbnail_url

0
votes

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() : '' )?> 
0
votes

You can get it like this:

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