Question

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?

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top