Determine if, within a post, you are on page 2 or greater - Wordpress documentation circular
I am trying to run a function that executes only on the first page of any post. So, if a post is paginated because you have used the !--nextpage--
tag and you are on page 2 or greater of that article, then the function should not run.
Even the Wordpress documentation is confused how to do this:
When the page being displayed is paged. This refers to an archive or the main page being split up over several pages and will return true on 2nd and subsequent pages of posts. This does not refer to a Post or Page whose content has been divided into pages using the nextpage QuickTag. To check if a Post or Page has been divided into pages using the nextpage QuickTag, see A_Paged_Page section.
Further details:
I currently use the following code in my functions.php to move my featured image within posts from the default position above the first paragraph and below the title, to underneath the first paragraph.
The problem is that it also shows the featured image, in the same spot, on pages 2, 3, 4, etc of the same article (when I have used !--nextpage--
). The featured image should only show on the first page of any article.
add_filter( 'the_content', 'insert_feat_image', 20 );
function insert_feat_image( $content ) {
if (!is_single(array(xxxxx))) {
global $post;
if (has_post_thumbnail($post-ID)) {
$caption = 'xxxx';
$img = 'xxx';
$content = xxx;
}
return $content;
} else {
$contentnormal = preg_replace('#(p.*?/p)#','$1'.$img . $caption, $content, 1);}
return $contentnormal;
}
I removed some potentially identifying code that is not relevant to this question (xxx).