treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] Wordpress: Pagination on a custom query on Homepage

  • Hello again, Guys!

    Hope you're all having a grand time. I wanted to have a pagination on a custom query that I wanted to show at the bottom of my homepage. Any ideas guys on how to do this? Please see attached link of my design that I wanted to look like

    http://i.imgur.com/SJ0AY.png

    Thank you advance :)

    All the best,

    Val
  • Thank you TheDoc! I did stumbled upon that blog and I can't seem to understand it (forgive me I'm not that advance in Wordpress :D )

    Here's the code I'm using in my index.php file


    <?php $recent = new WP_Query(); ?>
    <?php $recent->query('cat=5&showposts=10'); ?>
    <?php while($recent->have_posts()) : $recent->the_post(); ?>
    <h1><?php the_title(); ?></h1>
    <?php endwhile; ?>


    Can you tell me how it should it be?

    Thank you so much :)

    Val
  • Based on your image you would have to do something like this for your query:
    <?php $args = array(
    'posts_per_page' => 6,
    'paged' => get_query_var('paged')
    );
    query_posts( $args ); ?>

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- post -->
    <?php endwhile; ?>
    <!-- post navigation -->
    <?php else: ?>
    <!-- no posts found -->
    <?php endif; ?>

    And then you'll need to add in the pagination after your endwhile.
  • Ah.. ok will try the code and will let you know the output. Thanks TheDoc! :)
  • Hi TheDoc,

    I tried using the code you gave me, also added post navigation link, but the pagination doesn't seems to be working?

    Here's the code snippet:

    <?php $args = array(
    'posts_per_page' => 3,
    'paged' => get_query_var('paged')
    );
    query_posts( $args ); ?>

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

    <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    <?php endwhile; ?>
    <?php posts_nav_link(); ?>
    <?php else: ?>
    <?php endif; ?>

    Am I missing something?

    Thanks!

    Val
  • What happens when you click 'next'?
  • It just displays the same posts :| but I can see the site url changed to ?page=2 though. Am I missing anything?
  • Instead of:
    'paged' => get_query_var('paged')
    ...try:
    'paged' => get_query_var('page')
  • Ok got it. Will update you the output :)

    Thanks Doc!
  • Hi Doc,

    It didn't worked :|
  • Okay, mucho frustration!

    Let's try simplifying the loop and getting rid of the 'else' stuff and we'll add a reset:
    <?php

    $args = array(
    'posts_per_page' => 3,
    'paged' => get_query_var('paged')
    );

    // The Query
    query_posts( $args );

    // The Loop
    while ( have_posts() ) : the_post(); ?>

    <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>

    <?php endwhile;

    posts_nav_link();

    // Reset Query
    wp_reset_query();

    ?>
  • Hehe true, what would I do without CSS Tricks! :)

    Got it, will try again with the code you gave me.

    Thanks Doc!

    Val
  • Woohoo! Finally got it working, apparently I also have a main loop at the top part of my homepage I forgot to add <?php wp_reset_query();?> on that loop. Thanks so much Doc for the help, really appreciate it!

    Thank you CSS Tricks! :D

    Best,

    Val
  • But of course - should've asked about another loop! Glad you got it sorted.
  • Yeah my fault too. One thing though, what If I also add another pagination for my main loop, would that work? Or having 2 paginations on index.php doesn't work for Wordpress?
  • Honestly I've never tried it. Not sure!
  • I see, will try to look around more. I hope it works :)