WordPress.org

Ready to get started?Download WordPress

Forums

How to allow pagination when using wp_query? (8 posts)

  1. mattmikulla
    Member
    Posted 2 years ago #

    I have searched around and haven't found anything too specific on how to allow pagination to pages using wp_query.

    I am creating a blog of off my main homepage mysite.com/blog/ and was having problems using the normal loop. So I figured wp_query would be better.

    So far I can list the posts fine but am unsure what code I need to include in the template and query to enable pagination and output the pagination links.

    Any help?

    Here's the code I have so far. Remember, I'm not sure what to include to output pagination links:

    <?php	
    
    	$my_query = new WP_Query( array(
    	'post_status' => 'publish',
    	'post_type' => 'post',
    	'posts_per_page' => 2
    	) );
    
    	if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post();
    
    	?>
        <article class="excerpt media clearfix"> <a class="img" href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
          <?php
    		//This must be in one loop
    
    		if(has_post_thumbnail()) {
    			the_post_thumbnail("thumbnail-xs-c");
    		} else {
    			echo get_avatar( $id_or_email = get_the_author_meta('user_email'), $size = '75', $default = '<path_to_url>' );
    		}
    		?>
          </a>
          <div class="bd">
            <h2 class="h4"><a href="<?php the_permalink(); ?>">
              <?php  the_title(); ?>
              </a></h2>
            <?php the_excerpt(); ?>
          </div>
        </article>
    
        <?php
    		endwhile;
    		endif;
    		?>
  2. Jonas Grumby
    Member
    Posted 2 years ago #

    Are you just trying to show the 2 newest blog posts on a page? If so, can you go to Settings > Reading and just assign that as the main blog page? What problem were you having with the normal loop code? Have you looked at query_posts?

  3. mattmikulla
    Member
    Posted 2 years ago #

    First let me say I am a theming novice and I am still learning.

    Again, I am trying to create my blog off of my home page (mysite.com/blog/) that lists the most recent posts. Two was just a number I picked because I only have 3 posts in my test database and that would allow pagination as a test for now.

    Also, my home page is static and uses the index.php template.

    I originally tried the instructions at http://codex.wordpress.org/Making_Your_Blog_Appear_in_a_Non-Root_Folder but it does not work for me correctly.

    The code I posted above does work but I don't know how to get pagination to display or work with wp_query.

  4. deepbevel
    Member
    Posted 2 years ago #

  5. Jonas Grumby
    Member
    Posted 2 years ago #

    I am trying to create my blog off of my home page (mysite.com/blog/)

    Assuming that your WordPress installation is in the root folder and you are using "pretty permalinks" you can do that by creating a page called Blog and then in Settings > Reading choose that page as your main blog page and a different page as your home page. You can also set the number of posts to show there, without doing any coding. Then your blog page will come up as mysite.com/blog just like it does on this site and many others that I have set up.

    Also, I followed the link that you posted and you don't have to do all of that. Personally, I think that's pretty crazy advice on that page. I wonder if it's old. It seems like query_posts is more in vogue these days.

    <?php
    //The Query
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts('posts_per_page=2&paged=' . $paged);
    //The Loop
    if ( have_posts() ) : while ( have_posts() ) : the_post();
    
    ?>
  6. Jonas Grumby
    Member
    Posted 2 years ago #

    Also see http://codex.wordpress.org/Template_Hierarchy for php template naming conventions.

  7. deepbevel
    Member
    Posted 2 years ago #

    Ah yes, thanks mr. Grumby, and appolgies to matt for the convoluted thread, i must confess I posted in haste but at least they were dealing with the same issue, thought it might help.

  8. mattmikulla
    Member
    Posted 2 years ago #

    I apologize this forum post swerved to other topics.

    Thanks Jonas. I solved this issue about displaying posts withing the regular loop on my blog page.

    It's still good to have the above code example for reference.

    For further discussion of problems I am having with moving my blog off the main index (forum thread): http://wordpress.org/support/topic/blog-off-index-with-its-own-directory-broken-after-wordpress-update

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.