Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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
share|improve this answer
    
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. –  AndrettiMilas Jun 23 '13 at 16:03
    
@AndrettiMilas No worries, check out my revised answer. –  TheDeadMedic Jun 23 '13 at 21:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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