Question

I'm trying to display the full size image of the single post from a custom post type that I created, below is what I've tried

<?php
 $args = array(
    'post_type' => 'press',
    'order'         => 'ASC',
    'post_status'   => 'publish',
    'suppress_filters' => false
    );
$press = new WP_Query( $args );

if ( $press->have_posts() ) { ?>
    <?php while ( $press->have_posts() ) : $press->the_post(); ?>
    <div class="col-sm-4 mb24">
        <article class="post-article bg-white">
            <div class="article-img">
                <?php
                if( has_post_thumbnail() ){
                ?>
                <div class="img-box position-relative overflow-hidden">
                    <a href="<?php the_permalink(); ?>" class="height300 bg-graylight flex-center c-dark position-relative">
                        <img src="<?php echo get_the_post_thumbnail_url('full'); ?>" alt="<?php the_title(); ?>">
                    </a>
                </div>
                <?php } else { ?>
                <a href="<?php the_permalink(); ?>" class="height300 bg-graylight flex-center c-dark position-relative">
                    <i class="ti-image font64"></i>
                </a>
                <?php } ?>
            </div>
        </article>
    </div>
<?php endwhile; ?>
<?php } ?>
        </div>
    </div>
</section>

but seems this

get_the_post_thumbnail_url('full')

not working because it shows nothing e.g. src="" but if I do

get_the_post_thumbnail_url()

surely gives me the image url but not the full size one. Any help, ideas please?

Était-ce utile?

La solution

The first argument for that function is not size. See the documentation (if a function's not working, checking the documentation should always be your first step):

get_the_post_thumbnail_url( int|WP_Post $post = null, string|array $size = 'post-thumbnail' )

The first argument is which post the get the URL for, while the size is the second argument.

To avoid redundancy, when you're in the loop, set the first argument to null to get the current post:

<?php echo get_the_post_thumbnail_url( null, 'full' ); ?>

But honestly, you shouldn't be using this function this way. If you want to output the post thumbnail, use the the_post_thumbnail() function. That way you automatically get the image's alt text, width and height attributes, as well ass srcset and sizes:

<a href="<?php the_permalink(); ?>" class="height300 bg-graylight flex-center c-dark position-relative">
    <?php the_post_thumbnail( 'full' ); ?>
</a>
Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange
scroll top