0

I'm trying to find out the index of the post which is currently viewed (single post page).

I have an overview of the articles in the same category in a sidebar. But when a user navigates to a post which is located on page 2, the articles should now show up the articles of page 2, even when in single post page.

This is the code for the sidebar to show posts from the actual page:

    <ul>
    <?php

    $offset = 0;

    //THIS IS THE PROBLEMATIC PART

    if (is_single()) {
        $modulo = $wp_query->current_post % 6; // $wp_query->current_post somehow ever returns 0
        $offset = $wp_query->current_post - $modulo;
    }

    if (is_tag()) {
        $args = array(
            'posts_per_page'   => 6,
            'tag'              => get_query_var('tag') );
    }
    else {
        $args = array(
            'posts_per_page'   => 6,
            'category'         => $cat_id,
            'offset'           => $offset );
    }

    $myposts = get_posts( $args );
    $i = 0;
    foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
        <li>
            <?php if (is_blog()) { ?>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            <?php } else { ?>
            <a href="#" onclick="goTo(<?php echo $i; ?>);return false;"><?php the_title(); ?></a>
            <?php } ?>
        </li>
    <?php $i++; endforeach; ?>
    </ul>

As in the comments, $wp_query->current_post always returns 0, which I think is because it is outside a loop.

How can i solve that issue?

1

The problem of your code is that $wp_query is a global variable. Once the sidebar is loaded via a function (dynamic_sidebar) you need to globalize that variable before use.

However, once you are in the single view the current_post is always 0. You have to run another query, loop it, and check the every post id in this query against the original queried post id, until you find the right index.

Finally before use setup_postdata is a good practise globalize $post variable before loop start and reset post data (with wp_reset_postdata) after the loop end.

All previous tips putted together (untested):

<?php
// put this function in any accessible place, like functions.php or plugin...
function get_post_index ( $posts = array(), $vs = 0) {
  if ( empty($posts) ) return -1;
  $i = 0;
  foreach ( $posts as $one ) {
    if ( $one->ID == $vs ) return $i;
    $i++;
  }
  return -1;
}
?>

<ul>
<?php
$paged = 1;
if ( is_single() ) {
  // get ALL the posts in current category
  $all = get_posts( array('category' => $cat_id, 'posts_per_page' => -1) );
  // use the function 'get_post_index' to retrive the post index
  $index = get_post_index( $all, get_queried_object()->ID );
  if ( $index != -1 ) {
    global $wp_query;
    $perpage = $wp_query->get('posts_per_page') ? : get_option('posts_per_page');
    // get the paged value as ceil of index / posts_per_page
    // e.g. if index is 13 and posts_per_page is 6 it's = ceil(2.1666) = 3
    $paged = ceil( $index / $perpage );
    if ( $paged == 0 ) $paged = 1;
  }
}
$args = array('paged' => $paged );
if ( is_tag() ) {
  $args['tag'] = get_queried_object()->slug;
} else {
  $args['category'] = $cat_id;
}
$myposts = get_posts( $args );
if ( ! empty($myposts) ) :
  $i = 0;
  global $post;
  foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
  <li>
  <?php if (is_blog()) { ?>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
  <?php } else { ?>
    <a href="#" onclick="goTo(<?php echo $i; ?>);return false;"><?php the_title(); ?></a>
  <?php } ?>
  </li>
<?php $i++; endforeach; wp_reset_postdata(); endif; ?>
</ul>
  • Thanks for your input. But since i want to get the posts from a specific page, it wont work when you are on a single post page, because there is no query_var for the actual page. That isnt the whole code, i already checked for the category earlier and everything works. I have to do it with the offset so there is really only the index of a post which i need to finish that last step... – David Fariña Sep 16 '13 at 12:33
  • Thanks very much. I thought it may be possible without a loop, but that works at least. Thanks again and accepted – David Fariña Sep 16 '13 at 13:07
  • Edit 2: Also in get_post_index function, $all should be replaced with $posts... – David Fariña Sep 16 '13 at 13:20
  • @DavidFariña thanks again. Added a disclaimer for untested code... ;) – gmazzap Sep 16 '13 at 13:22
0

Using the $wp_query object, you can get the ID of the post outside the loop from post->ID

    global $wp_query;
    $thePostID = $wp_query->post->ID;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.