1

In my posts I'm using <!--nextpage--> in order to paginate specific blog posts, which works fine. But my issue is that the featured image shows in the post header for each page.

I've tried using !is_paged but it's not working and I read that it doesn't work for posts using <!--nextpage-->

The code I'm using:

if (isset($props['featured_in_head'])) {
  if ( !is_paged() ){
    $props['featured'] = $props['featured_in_head'];
  }
}

I've noticed on the sub-pages of this one post that my body has the classes of single-paged-2 or paged-2, but it doesn't look like it has just the paged class, so if there's a way to hide .featured via CSS I could just do that

Any help is appreciated

1 Answer 1

3

I read that it doesn't work for posts using <!--nextpage-->

Not necessarily, but basically, if the current URL ends with /page/<number> as in https://example.com/page/2/ and https://example.com/category/foo/page/2/, then is_paged() would work, i.e. it returns true on page 2, 3, etc.

But for singular posts with a page break, i.e. the <!--nextpage--> tag, where the URL normally ends with /<number> as in https://example.com/hello-world/2/, then is_paged() would return a false.

However, you can use get_query_var( 'page' ) (or the global $page variable) to get the page number and then render the featured image only if the page number is 1 or < 2.

So for example, just replace the !is_paged() in your if with (int) get_query_var( 'page' ) < 2, and then that image would no longer appear on page 2, 3 and so on.

1
  • 1
    Perfect solution, thank you so much. The confusion is cleared up now for sure, but I wasn't even aware of the get_query_var option so that's another tool in my belt now. Thanks!
    – Geoff_S
    Commented Apr 13, 2022 at 19:30

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.