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.

I'm wondering if there is a good way to tell which archive page a post came from. I essentially just need the post's position in the total order, then divide it by the 'posts_per_page' option. The hangup I'm having is getting that position or offset of where the post sits.

EDIT: All while being on the SINGLE POST template.

There, no matter what, the usual $wp_query global and 'page'/'paged' query vars are always 0 - so those won't get me anywhere.

share|improve this question

4 Answers 4

If you're on page 2+, you'll have access to the global $page, which should tell you the pagenumber you're currently on. There're also some others that are worth a look (never know where which one is present/active): global $multipage, $numpages;.

You can also check the current page via the get_query_var(); core function. Basically the global $page comes from there.

// Using get_query_var()
$paged = get_query_var('page') ? get_query_var('page') : false;

// Or via the global wp_query object
$paged = $GLOBALS['wp_query']->query_vars['page'] ? $GLOBALS['wp_query']->query_vars['page'] : false;

if ( $paged )
    // do stuff - @example echo "Current page is {$paged}";
share|improve this answer
    
nice, I didn't know about these globals! -- I should have been more specific in my question though. I'm trying to find which page it belongs to while on the single template. -- my answer to my own question works, but I'm wondering if there's a better way. –  Drew Gourley Oct 7 '11 at 13:21
    
Easy: See update. –  kaiser Oct 7 '11 at 16:54
up vote 0 down vote accepted

Alright, nevermind. I got this solved by doing this at the top of the single post template:

$position_query = array( 'post_type' => 'portfolio', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => -1 );
$position_posts = get_posts($position_query); $count = 0;
foreach ($position_posts as $position_post) { $count++;
    if ($position_post->ID == $current_id) { $position = $count; break; }
}
$posts_per_page = get_option('posts_per_page');
$result = $position/$posts_per_page;
$current_page = ceil($result);
share|improve this answer

I needed something like this for singles only and slightly simplified the accepted answer:

  $position_query = array( 'post_type' => 'post','order'=>'DESC', 'numberposts' => -1 );
  $position_posts = get_posts($position_query); $count = 0;

  foreach ($position_posts as $position_post) { $count++;
        if ($position_post->ID == $post->ID) { $current_page = $count; break; }
   }

  $total_posts = count($position_posts);

This can used in this context: 'Page 1 of 10' 1 being $current_page and 10 being $total_posts

share|improve this answer

We have 2 Conditional Tags to make this simpler

 <?php if(is_single())
    {
    echo "post";
    }
    elseif(is_page())
    {
    echo "page";
    } ?>

Paste This in the File which includes in both page and post and changed from "post" & "Page" to whatever your need is.

share|improve this answer

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.