WordPress.org

Support

Support » Themes and Templates » [Resolved] Pagination with custom post type listing

[Resolved] Pagination with custom post type listing

  • I am fairly new to wordpress and a total PHP newbie… What I want to be able to do is display 9 post thumbnails from a custom post type, and have a pager at the bottom. Here is an excerpt of my template code:

    <div id="content" role="main" class="entry-content">
    
    <?php $loop = new WP_Query( array( 'post_type' => 'accessories', 'posts_per_page' => 9, 'orderby'=> menu_order) ); ?>    
    
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <div class="accessory_image"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) { the_post_thumbnail(array(200,160)); } ?></a>
            <?php the_title( '<h3 class="accessory-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h3>' ); ?>
    </div>
    <?php endwhile; ?>
    <div class="navigation">
      <div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>
      <div class="alignright"><?php next_posts_link('More &raquo;') ?></div>

    The “more” and “previous” links are there, and the “more” link does point to “our_accessories/page/2” (I realized I couldn’t name the page the same as the content type)

    But the same items show on each page.

    Anyone have any insight?

    Thanks!!

    Dani

Viewing 15 replies - 1 through 15 (of 44 total)
  • See if this works:

    change:

    <?php $loop = new WP_Query( array( 'post_type' => 'accessories', 'posts_per_page' => 9, 'orderby'=> menu_order) ); ?>

    to

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $loop = new WP_Query( array(
    'post_type' => 'accessories',
    'posts_per_page' => 9,
    'orderby'=> 'menu_order'
    'paged'=>$paged
    ) ); ?>

    Michael – Thank you so much! You saved my sanity. (PS you were missing one of those pesky commas at the end of ‘menu_order’, but I still love you anyway!)

    Gotta love those “D’oh” moments when in hindsight the answer seems so clear… 🙂

    Thanks again.

    I am having the same problem but your suggested solution is not working for my custom post types.

    What am I doing wrong?

    <?php
    /*
    Template Name: Wallpaper
    
     * @author Harmeet Gabha
     * @copyright 2010
     */
    
    ?>
    <?php get_header(); ?>
    	<div id="content">
    	<?php
    		$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    		echo "paged " . $paged;
    		$loop = new WP_Query(array('post_type' => 'wallpaper',
    									'post_status' => 'published',
    									'paged' => $paged,
    									'posts_per_page' => 8,
    									'orderby' => 'menuorder',
    									'caller_get_posts' => 1
    									));
    	?>
    	<div id="wallpaper-container">
    	<ul class="blog-wallpaper">
    	<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    	<?php
    		//$custom = get_post_custom($post->ID);
    		//$screenshot_url = $custom["screenshot_url"][0];
    		//$website_url = $custom["website_url"][0];
    	?>
    
    	<?php ob_start();the_post_thumbnail();$toparse=ob_get_contents();ob_end_clean();?>
    	<? preg_match_all('/src=("[^"]*")/i', $toparse, $img_src); $thumbnail = str_replace("\"", "", $img_src[1][0]);?>
    
    			<li id="blog-<?php the_ID(); ?>">
    			<em><a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
    
    			</em>
    			<a href="<?php the_permalink() ?>">
    			<img src="<?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=<?php echo $thumbnail; ?>&w=200&h=150&zc=1" alt="image" />
    			</a>
    			</li>
            <?php endwhile; ?>
    		</ul>
    	</div>
    
    <div class="navigation">
      <div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>
      <div class="alignright"><?php next_posts_link('More &raquo;') ?></div>
      </div>
    
        </div><!-- #content -->
    	<?php get_sidebar();?>
    <?php get_footer(); ?>

    Try this

    <?php
          /*
          Template Name: Wallpaper
    
           * @author Harmeet Gabha
           * @copyright 2010
           */
    
          ?>
          <?php get_header(); ?>
          	<div id="content">
          	<?php
          		$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
          		echo "paged " . $paged;
          		$loop = new WP_Query(array('post_type' => 'wallpaper',
          									'post_status' => 'published',
          									'paged' => $paged,
          									'posts_per_page' => 8,
          									'orderby' => 'menu_order',
          									'caller_get_posts' => 1
          									));
          	?>
          	<div id="wallpaper-container">
          	<ul class="blog-wallpaper">
          	<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
          	<?php
          		//$custom = get_post_custom($post->ID);
          		//$screenshot_url = $custom["screenshot_url"][0];
          		//$website_url = $custom["website_url"][0];
          	?>
    
          	<?php ob_start();the_post_thumbnail();$toparse=ob_get_contents();ob_end_clean();?>
          	<? preg_match_all('/src=("[^"]*")/i', $toparse, $img_src); $thumbnail = str_replace("\"", "", $img_src[1][0]);?>
    
          			<li id="blog-<?php the_ID(); ?>">
          			<em><a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
    
          			</em>
          			<a href="<?php the_permalink() ?>">
          			<img src="<?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=<?php echo $thumbnail; ?>&w=200&h=150&zc=1" alt="image" />
          			</a>
          			</li>
                  <?php endwhile; ?>
          		</ul>
          	</div>
    
          <div class="navigation">
            <div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>
            <div class="alignright"><?php next_posts_link('More &raquo;') ?></div>
            </div>
    
              </div><!-- #content -->
          	<?php get_sidebar();?>
          <?php get_footer(); ?>

    Thanks Chinmoy. The only difference I see is
    ‘orderby’ => ‘menuorder’,
    changed to
    ‘orderby’ => ‘menu_order’,

    This doesn’t make any difference. I can even remove the orderby parameter which has the same result.

    $loop = new WP_Query(array('post_type' => 'wallpaper',
          									'post_status' => 'published',
          									'paged' => $paged,
          									'posts_per_page' => 8,
          									'orderby' => 'menu_order'
          									));

    put it once and check once

    this code worked for me:

    <?php
    global $wp_query;
    query_posts( array('post_type' => array( 'postypename' ),'showposts' => 3, 'paged'=>$paged )
    );?>

    thanks everyone!

    Ian

    @ianaleksander

    I’m having a possibly related problem. My next page links work, but the title displays as “No posts found” once it moves out of the first page.

    http://www.tradereadingorder.com/list/comics/dc-comics-universe/page/2/

    any ideas?

    Me too, the next link is displayed but once move to 2nd page, it shows “No posts found”.

    What’s missing?

    Looks like this (404 in 2nd page) is happening when I use /%category%/%postname%/ as permalink. When I use /%postname%/ as permalink, the problem is gone.

    I guess, a link like:

    http://mydomain.com/product/page/2/ – {product} is post_type

    is assumed by WordPress to have ‘product’ as category slug, and ‘page’ as post slug.

    Any ideas or fix to this ‘bug’?

    After I look more detail, it’s NOT that simple. With different combination of the following setting values when registering custom post_type, the result is unpredictable (200 or 404):

    1. permalink structure
    2. array( 'rewrite' => true ) // or false, as one of $args values in register_post_type() function call
    3. array( 'taxonomies' => array('category') ), as as one of $args values in register_post_type() function call

    I found that, setting the 'rewrite' => false so far is safe, although the resulting permalink is not that pretty.

    Example: with 'rewrite' => true, permalink is generated as follow:

    http://mydomain.com/product/my-product-name/

    But, if it is set to false the permalink is:

    http://mydomain.com/?product=my-product-name

    Any suggestions?

    Try this plugin this fixes similar issue of 404 which category pagination : http://wordpress.org/extend/plugins/category-pagination-fix/

    When you use /%category%/%postname%/ as permalink

    Ian

    @ianaleksander

    awesome rahnas, I’ll give that a try. thank you!

    Ian

    @ianaleksander

    ah well. Didn’t seem to fix my problem. The posts are displaying correctly, but the TITLE is still “no posts found for so and so”

    I’ve tried all of the codes above and nothing seems to work.. the pagination links simply don’t show up. This is what I have now:

    <?php $loop = new WP_Query( array( 'post_type' => 'blog', 'posts_per_page' => 10 ) ); ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    
    <h2 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    
    <?php the_content(); ?>
    
    <?php endwhile; ?>
    
    <div class="navigation">
    <div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>
    <div class="alignright"><?php next_posts_link('Next &raquo;') ?></div>
    </div>
Viewing 15 replies - 1 through 15 (of 44 total)
  • The topic ‘[Resolved] Pagination with custom post type listing’ is closed to new replies.
Skip to toolbar