WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Next_post_link and Previous_post_link not working (3 posts)

  1. sclausen
    Member
    Posted 3 years ago #

    In my header i've got this code:

    <?php
    	$args = array( 'numberposts' => 1, 'orderby' => 'recent', 'cat' => '-7,-8,-9');
    	$rand_posts = get_posts( $args );
    	foreach( $rand_posts as $post ) : ?>
    		<a href="<?php the_permalink(); ?>">
    		<?php echo get_the_post_thumbnail($post->ID); ?>
    		<div id="newestpost" class="bg50 absolute">
    		<h1 href="<?php the_permalink(); ?>"><?php the_title(); ?></h1>
    		</div></a>
    	<?php endforeach; ?>

    And this code does something, to the next and previous links..

    If i remove it, the links work again.
    But i dont want to remove the feature..

    Can anyone help?

  2. alchymyth
    Forum Moderator
    Posted 3 years ago #

    your foreach loop uses the $post variable, which is also a global variable used in the original query and in the loop;

    try to reset the data at the end of your code, by ading wp_reset_query(); directly after the endforeach;

  3. sclausen
    Member
    Posted 3 years ago #

    Dude! Thank you! :D

Topic Closed

This topic has been closed to new replies.

About this Topic