WordPress.org

Forums

Make own blog page template with pagination (5 posts)

  1. Typografitti
    Member
    Posted 1 year ago #

    Hi WP developers.

    Trying to get my first WordPress with bootstrap 3 running. I'm stuck in the pagination of my template that won't function. Someone who please can help. Thanks /peter.

    The code for the template.

    <?php
    /*
    Template Name: Rompa
    */ ?>

    <!-- HUVUD ------------------------------------------------------------------------------------------->

    <?php get_header(); ?>

    <h1 class="tunn grey" style="clear: both;"><?php the_title(); ?></h1>

    <div class="blogg col-xs-12 col-sm-10 col-md-10">
    <?php
    $args = array( 'posts_per_page' => 2 );
    $lastposts = get_posts( $args );
    foreach ( $lastposts as $post ) :
    setup_postdata( $post ); ?>

    <div class="info col-xs-5 col-sm-2 col-md-2">
    <div class="blogdate col-xs-0 col-sm-0 col-md-0">
    <p class="post-day"><?php the_time('j') ?> </p>
    <p class="post-month"><?php the_time('F') ?> </p>
    <p class="post-year"><?php the_time('Y') ?> </p>
    </div>
    </div>
    <div class="post col-xs-12 col-sm-10 col-md-10"><h1 class="grey">"><?php the_title(); ?></h1>
    <?php the_content(); ?>
    </div>
    <div class="tunn"></div>
    <?php endforeach; wp_reset_postdata(); ?>
    <!-- Add the pagination functions here. -->
    <?php posts_nav_link(); ?>
    </div>
    <div class="blogg col-xs-12 col-sm-12 col-md-2 pull-right">
    <?php get_sidebar(); ?>
    </div>

    <?php get_footer(); ?>

  2. vtxyzzy
    Member
    Posted 1 year ago #

    The posts_nav_link() function is not meant to provide pagination of multiple posts, but rather of a single post divided into multiple pages. You probably need previous_posts_link() and next_posts_link().

    However, if you are truly trying to have the blog page use a template, that will not work either. The blog page ignores templates as explained in this Codex article: http://codex.wordpress.org/Settings_Reading_Screen

    You can create a home.php file to show a custom blog page. You should create a Child theme to make your changes. If you do not, all of your changes will be lost if you update your theme.

  3. Typografitti
    Member
    Posted 1 year ago #

    I´ve now tried a different way by a WP_query. But if you say its not possible with templates I´ve to think a different way. I´ve to add that I have not chosen any page for the blog in the setting of reading screen. If it matters?

    <?php
    /*
    Template Name: Blog3
    */
    ?>
    
    <?php $args = array('');
    $pages = get_pages($args);?>
    <?php // The Query
    $query1 = new WP_Query( $args );
    ?>
    
    <?php // The Loop
    while ( $query1->have_posts() ) {
    	$query1->the_post();
    	echo '<h1>' . get_the_title() . '</h1>';
    	echo '<p>' . the_excerpt() . '</p>';
    }
    ?>
    
    <?php
    // get_next_posts_link() usage with max_num_pages
    echo get_next_posts_link( 'Older Entries', $the_query->max_num_pages );
    echo get_previous_posts_link( 'Newer Entries' );
    ?> 
    
    <?php /* Restore original Post Data
     * NB: Because we are using new WP_Query we aren't stomping on the
     * original $wp_query and it does not need to be reset.
    */
    wp_reset_postdata();
    wp_reset_query();
    
    ?>
  4. vtxyzzy
    Member
    Posted 1 year ago #

    You should create a Page and choose it as the Blog page. Then, instead of creating a template, create a home.php file. But, you should do that in a child theme or else your changes will be lost if you update the theme.

  5. Typografitti
    Member
    Posted 1 year ago #

    Thanks for your support in this issue, that has coasted a lot of frustation and made my hair turn grey . I´ve tried with your suggestion, but the links didn´t work anyway.

    But this solved the problem for me
    http://digwp.com/2013/01/display-blog-posts-on-page-with-navigation/

Topic Closed

This topic has been closed to new replies.

About this Topic