today, i need your help.

On my pages i have a sidebar with :

        <div class="sidebarActu">
            <ul>
            <?php
            $i = 1;
            while ( have_posts() ) : the_post();?>
                <li <?php if($i == 1){?>class="selected"<?php } ?> > <?php get_template_part( 'template-parts/content1', get_post_format() );?> </li> <?php
            $i++;
            endwhile; ?>
            <li class="all"><?php echo previous_posts_link();?> | <?php echo next_posts_link(); ?></li>
            </ul>
        </div>

This works perfectly !

Now, on my single.php, this not works because “have_posts()” return 1 …

So i tried something :

        <div class="sidebarActu">
            <ul>
            <?php
            $i = 0;
            $posts = get_posts();
            foreach($posts as $post){
                $pid = $post->ID;
                $date = $post->post_date;
                $date = date_create($date);
                $date = date_format($date, 'd/m/y');
                ?>

                <li <?php if($id == $pid){?>class="selected"<?php } ?> ><h2 class="entry-title"><a href="<?php get_site_url(); echo $post->post_name; ?>"><?php echo $post->post_title; ?></a></h2><span><?php echo $date; ?></span></li>
                <?php
                if (++$i == 4) break;
            }
            ?>
            </ul>
        </div>

Works perfectly, but can’t have pagination.

Some one can help me ?

Thanks a lot

Read more here: Posts loop with pagination on a single post page


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: