• Resolved robce73

    (@robce73)


    hello,
    I am building a template from scratch.
    i have a problem with the post-navigation on my front-page.php (static page).
    it will always return the same posts…

    If i change in Admin->Settings->Reading->Front page ->displays to ->Your latest posts the same code works perfectly.

    What am I doing wrong?
    front-page.php:

    <?php
    /**
     * Template Name: Home Template
     */
    
    get_header(); ?>
    <?php
    
    if (have_posts()) : while (have_posts()) : the_post(); ?>
       //home page stuff
    
    <?php endwhile; else: ?>
        //
    <?php endif; ?>
    
    <div id="site-content">
            <div id="list">
                <?php
    
                $temp = $wp_query;
                $wp_query = null;
                $wp_query = new WP_Query();
                $wp_query->query('showposts=5' . '&paged=' . $paged);
                ?>
                <?php while ($wp_query->have_posts()) :
                    $wp_query->the_post(); ?>
                    <?php get_template_part('content', get_post_format()); ?> <?php endwhile; ?>
    
            </div>
        </div>
    <?php if ($paged > 1) { ?>
    
        <nav id="nav-posts">
            <div class="prev"><?php next_posts_link('&laquo; Previous Posts'); ?></div>
            <div class="next"><?php previous_posts_link('Newer Posts &raquo;'); ?></div>
        </nav>
    
    <?php } else { ?>
    
        <nav id="nav-posts">
            <div class="prev"><?php next_posts_link('&laquo; Previous Posts'); ?></div>
        </nav>
    
    <?php } ?>
    
    <?php wp_reset_postdata(); ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Moderator keesiemeijer

    (@keesiemeijer)

    Also see this example from next_posts_link how to use it with a custom query: https://codex.www.ads-software.com/Function_Reference/next_posts_link#Usage_when_querying_the_loop_with_WP_Query

    Here is an example:

    <?php
    /**
     * Template Name: Home Template
     */
    
    get_header(); ?>
    <?php
    
    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
       //home page stuff
    
    <?php endwhile; else: ?>
    
    <?php endif; ?>
    
    <div id="site-content">
            <div id="list">
                <?php
    $paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
    $the_query = new WP_Query( 'posts_per_page=5&paged=' . $paged );
    ?>
                <?php while ( $the_query->have_posts() ) :
        $the_query->the_post(); ?>
                    <?php get_template_part( 'content', get_post_format() ); ?> <?php endwhile; ?>
    
            </div>
        </div>
    <?php if ( $paged > 1 ) { ?>
    
        <nav id="nav-posts">
            <div class="prev"><?php next_posts_link( '? Previous Posts', $the_query->max_num_pages ); ?></div>
            <div class="next"><?php previous_posts_link( 'Newer Posts ?' ); ?></div>
        </nav>
    
    <?php } else { ?>
    
        <nav id="nav-posts">
            <div class="prev"><?php next_posts_link( '? Previous Posts', $the_query->max_num_pages ); ?></div>
        </nav>
    
    <?php } ?>
    
    <?php wp_reset_postdata(); ?>

    Thread Starter robce73

    (@robce73)

    Wow..Thank you for your very fast answer! Everything works great with your help.
    Greetings
    robce

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘post-navigation on front-page’ is closed to new replies.