• Resolved cupaball

    (@cupaball)


    I copied some code to pull regular post from who knows where a while ago and now I can’t get (1) get it to work with the simple loop and (2) I can’t get it to paginate. I am wondering what is the difference from this

    $args = array('posts_per_page' => 3 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();

    and this

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php the_content(); ?>
    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>

    For some reason when I use the 2nd code, it only displays the title of the page. And for some reason I am having difficulty understanding where to place the pagination code.

    I am just looking to add pagination to my page.

    The page is https://www.grayareabrand.com/blog/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Use the following code just before “<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>” in your code:

    query_posts( 'posts_per_page=3' );

    And Place the paging code after “endif” statement.

    Thread Starter cupaball

    (@cupaball)

    Thanks, chandanonline4u. My only issue now is what is the paging code? I tried to copy from the 2012 code, but I’m not sure which code. Also, if I use query_posts(); will the number of post be based on the what is in the reading admin panel?

    Use the following code just after loop:

    <?php
    global $wp_query;
    
    if ( $wp_query->max_num_pages > 1 ) : ?>
      <nav id="<?php echo $nav_id; ?>">
        <h3 class="assistive-text"><?php _e( 'Post navigation', 'twentyeleven' ); ?></h3>
        <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'twentyeleven' ) ); ?></div>
        <div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'twentyeleven' ) ); ?></div>
      </nav><!-- #nav-above -->
    <?php endif; ?>

    No, If you define “posts_per_page” argument in query_posts then it will override reading settings in admin panel.

    Thread Starter cupaball

    (@cupaball)

    Nice! So here is what I have

    <section id="vid_content">
    <?php
    query_posts( 'posts_per_page=3' );
    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php echo'<article class="vids">';
    	echo'<h3>';
    	the_title();
    	echo'</h3>';
    	the_content();
    	echo'
    	<p>#thatshitgray <a href="https://www.twitter.com/grayareabrand">@grayareabrand</a> |
    	<a href="https://www.facebook.com/grayareabrand">Facebook </a></p>';
    	echo'</article>'; ?>
    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>
    
    <?php
    global $wp_query;
    
    if ( $wp_query->max_num_pages > 1 ) : ?>
      <nav id="<?php echo $nav_id; ?>">
        <h3 class="assistive-text"><?php _e( 'Post navigation', 'twentyeleven' ); ?></h3>
        <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'twentyeleven' ) ); ?></div>
        <div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'twentyeleven' ) ); ?></div>
      </nav><!-- #nav-above -->
    <?php endif; ?>
    
    </section>

    When I click the next button, the page navigates to the next page but it shows the same post. Weird right? Here is the page so you can make sure I’m not crazy. https://www.grayareabrand.com/blog/

    Replace the currently used query_posts code with the following code:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts(array('posts_per_page'=>3,'paged'=>$paged));

    And Place paging code just before closing endif statement.

    Thread Starter cupaball

    (@cupaball)

    Thank you very much! Solve my issue. In case anyone needs the final code here it is:

    <section id="vid_content">
    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts(array('posts_per_page'=>3,'paged'=>$paged));
    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php echo'<article class="vids">';
    	echo'<h3>';
    	the_title();
    	echo'</h3>';
    	the_content();
    	echo'
    	<p>#thatshitgray <a href="https://www.twitter.com/grayareabrand">@grayareabrand</a> |
    	<a href="https://www.facebook.com/grayareabrand">Facebook </a></p>';
    	echo'</article>'; ?>
    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>
    
    <?php
    global $wp_query;
    
    if ( $wp_query->max_num_pages > 1 ) : ?>
      <nav id="<?php echo $nav_id; ?>">
        <h3 class="assistive-text"><?php _e( 'Post navigation', 'twentyeleven' ); ?></h3>
        <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'twentyeleven' ) ); ?></div>
        <div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'twentyeleven' ) ); ?></div>
      </nav><!-- #nav-above -->
    <?php endif; ?>
    
    </section>
    Thread Starter cupaball

    (@cupaball)

    Got it working!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘The Loop and Pagination’ is closed to new replies.