0

In WordPress a single post can be divided into multiple pages using page-links.

Currently, I use featured images on my post pages and call them by size, as shown below. If an image is larger than 500 pixels it is displayed as the top-post thumbnail and if it is smaller than that it displays as the standard thumbnail.

<?php $image_data = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ),  'full' );
if ( $image_data[1] >= 500 ) { the_post_thumbnail('top-post');
} ?>

<?php $image_data = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ),  'full' );
if ( $image_data[1] < 500 ) { the_post_thumbnail();
} ?>

The problem is when I include this in my single-post.php it shows up on every page of a post, even when that post is divided by page-links. How do I make these images only show on the first page?

1 Answer 1

2

Use the global $page, which holds the current page number.

// Somewhere near the top
if ( $GLOBALS['page'] === 1 && $image_data = get_post_thumbnail_id() ) {
    if ( $image_data = wp_get_attachment_image_src( $image_data,  'full' ) ) {
        if ( $image_data[1] >= 500 )
            the_post_thumbnail( 'top-post' );
    }
}

// Further down. $image_data still exists, no need to grab it again.
if ( $GLOBALS['page'] === 1 && $image_data && $image_data[1] < 500 )
    the_post_thumbnail();

You'll see I also a did a little spring cleaning:

  1. Removed redundant second $image_data = ...
  2. Added check that there is indeed a thumbnail
  3. Added check that $image_data exists
2
  • I actually need two separate codes because each thumbnail size is placed somewhere else in the HTML mark-up. I assume I'd need to use two codes like this snippi.com/s/sbj17s8 ; but how do I make it so that no image display if the size condition isn't met in each. Commented Jun 23, 2013 at 16:03
  • @AndrettiMilas No worries, check out my revised answer. Commented Jun 23, 2013 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.