• Resolved osamaibra

    (@osamaibra)


    the user can insert a <!–nextpage–> to add a break to his post, in my plugin i want to determine witch is the first page, i tried to use is_paged() with no success, when i went to see on function reference is_paged(), i saw that is_paged doesn’t work qith the quick tag <!–nextpage–>.
    i need this to load template for the first post page and for the others pages if exists another one.
    how can i do that?!!
    thanks in advance.
    **sorry for my english! ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • There is a global $pages variable that contains the content of the page as an array of each page.

    So inside the loop:

    global $pages;
    $first_page = $pages[0];

    To get the first page of a post with the id, say, 33, outside the loop, you can do:

    global $post;
    $post = get_post( 33 );
    setup_postdata( $post );
    global $pages;
    $first_page = $pages[0];
    wp_reset_postdata();

    Edit: I believe I’ve misunderstood your question slightly.That’s how to get the first page, not determine the one you’re on. I think all you need is:

    global $page;
    if ( $page == 1 ) {
    // Do something
    }
    Thread Starter osamaibra

    (@osamaibra)

    Hi, thank you very much for your quick answer,

    global $page;
    if ( $page == 1 ) {
    // Do something
    }

    $page in the first page returned 0 & for the second returned.
    is it normal?!
    for me the first page is $page==0,is it right?

    sorry again for the english!

    That code will only work inside the loop. (so after the_post(); and before endwhile;.

    The good thing is that outside the loop it still picks up pages 2 & 3 and so on. So you just need to check:

    global $page;
    if ( $page == 1 || $page == 0 ) {
        // Do something
    }

    Only problem you might run into is if you want the first page of a multi-page page to look different to pages that only have one page. Since $page also equals zero when there are no pages.

    You’d need to do something like this to account for that:

    while( have_posts() ) : the_post();
        global $pages;
        $page_count = count( $pages );
    endwhile;
    
    global $page;
    
    if ( $page_count > 1 && ( $page == 0 || $page == 1 ) ) {
        // First page of multi-page page
    } else {
        // Page only has one page
    }

    Thread Starter osamaibra

    (@osamaibra)

    sorry for answering to late,
    thank you for the non paginated posts point and it did the trick.
    it was really very helpful!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to get first post page in a paginated post with’ is closed to new replies.