Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Josh Eccard
Josh Eccard
9,261 Points

Older Posts Nav

Maybe I missed it somewhere in the WP Theme project, but I am trying to limit my posts to displaying only 3-5 per page. However when I do this in the settings, no navigation to the older posts shows up. I have tried using the <?php next_posts_link(); ?> and etc... but can't get it to show up.

2 Answers

Matt Campbell
Matt Campbell
9,767 Points

Hi If you want proper pagination, use this in your functions.php file

if ( ! function_exists( 'pagination' ) ) :
function pagination() {
    global $wp_query;

    $big = 999999999; 

    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $wp_query->max_num_pages
    ) );
}
endif;

Then call it in your template file with this -

    <?php real_pagination(); ?>

I wrap that in a div and add this CSS

/******************* PAGINATION *******************/
.post-navigation{
margin-top:50px;
font-family:Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif;
color:#2AC0FF;
}

.post-navigation a{
display:inline-block;
margin-top:25px;
}

.post-navigation a:first-child{
padding-right:15px;
margin-right:15px;
border-right:1px solid #2AC0FF;
}

.post-navigation a:last-child{
padding-left:15px;
margin-left:15px;
border-left:1px solid #2AC0FF;

}

It results in this: http://ultimategamingparadise.com/reviews/

Look at the bottom of the page.

Excuse the state of the site, it's been neglected. I have a massive update coming for it.

Josh Eccard
Josh Eccard
9,261 Points

Thank you but I'm still having problems. Under the admin panel I have it set to 5 posts per page. And below is my code that is now only showing 1 post per page and no navigation links. Here is my code:

functions.php file

<?php

// Load the Theme CSS function theme_styles() {

wp_enqueue_style( 'normalize', get_template_directory_uri() . '/css/normalize.css' );
wp_enqueue_style( 'grid', get_template_directory_uri() . '/css/grid.css' ); 
wp_enqueue_style( 'main', get_template_directory_uri() . '/style.css' );

}

add_action( 'wp_enqueue_scripts', 'theme_styles' );

// Enable custom menus add_theme_support( 'menus' );

// Function for creating Widegets function create_widget($name, $id, $description) {

register_sidebar(array(
    'name' => __( $name ),   
    'id' => $id, 
    'description' => __( $description ),
    'before_widget' => ' ',
    'after_widget' => ' ',
    'before_title' => '<h5>',
    'after_title' => '</h5>'
));

}

// Create widgets in the footer create_widget("Left Footer", "footer_left", "Displays in the left of the footer"); create_widget("Middle Footer", "footer_middle", "Displays in the middle of the footer"); create_widget("Right Footer", "footer_right", "Displays in the right of the footer");

// Pagination if ( ! function_exists( 'pagination' ) ) : function pagination() { global $wp_query;

$big = 999999999; 

echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $wp_query->max_num_pages
) );

} endif;

?>

front-page.php file

<?php

/*

Template Name: Home Page

*/

get_header(); ?>

<?php

    $args = array(
            'post_type' => 'homepost'
        );

    $the_query = new WP_Query ( $args );

?>

<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    <h2><?php the_date(); ?> - <?php the_title(); ?></h2>
    <?php the_field( 'image' ); ?>
    <blockquote><?php the_field( 'content' ); ?></blockquote>

    <h4><a href="<?php the_permalink(); ?>">Click to view comments.</a></h4>

    <?php real_pagination(); ?>

    <hr>

<?php endwhile; else: ?>

    <p>There are no posts.</p>

<?php endif; ?>

<?php get_sidebar(); ?>

<?php get_footer(); ?>