using is_paged for hiding image on posts

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

Topic paged pagination posts Wordpress

Category Web


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.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.