WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Pagination with custom post type listing (45 posts)

  1. dkristine
    Member
    Posted 3 years ago #

    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

  2. MichaelH
    Member
    Posted 3 years ago #

    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
    ) ); ?>
  3. dkristine
    Member
    Posted 3 years ago #

    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.

  4. scorpion380
    Member
    Posted 3 years ago #

    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(); ?>
  5. chinmoy
    Member
    Posted 3 years ago #

    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(); ?>
  6. scorpion380
    Member
    Posted 3 years ago #

    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.

  7. chinmoy
    Member
    Posted 3 years ago #

    $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

  8. eleven.mary
    Member
    Posted 3 years ago #

    this code worked for me:

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

    thanks everyone!

  9. Ian
    Member
    Posted 3 years ago #

    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?

  10. Agus Suhanto
    Member
    Posted 3 years ago #

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

    What's missing?

  11. Agus Suhanto
    Member
    Posted 3 years ago #

    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'?

  12. Agus Suhanto
    Member
    Posted 3 years ago #

    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?

  13. Remiz Rahnas
    Member
    Posted 3 years ago #

    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

  14. Ian
    Member
    Posted 3 years ago #

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

  15. Ian
    Member
    Posted 3 years ago #

    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"

  16. Liesl Fawcett
    Member
    Posted 3 years ago #

    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>
  17. TakeoK
    Member
    Posted 3 years ago #

    I got the prev/next links to show up using this code:
    http://weblogtoolscollection.com/archives/2008/04/19/paging-and-custom-wordpress-loops/

    but when I click them I get a 404. Here are some details:

    • I created a custom loop for a custom post type (carvings)
    • I'm using the "day and name" permalink structure
    • I installed the category pagination fix plugin
    • The paging links look like http://mydomain.com/carvings/page/2/

    No dice. Very aggravating and I refuse to use ugly links for SEO reasons.

    Can someone mark this thread unresolved? Probably only the original posted I guess.

  18. TakeoK
    Member
    Posted 3 years ago #

    p.s. here is my actual code

    <?php
    $temp = $wp_query;
    $wp_query = null;
    $wp_query = new WP_Query();
    $wp_query->query('post_type=carvings' . '&paged=' . $paged . '&posts_per_page=12');
    ?>
    <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
    
    <!-- output --> 
    
    <?php endwhile; ?>
    <?php previous_posts_link(); ?>
    <?php next_posts_link(); ?>
    <?php $wp_query = null; $wp_query = $temp; ?>
  19. Harish Chouhan
    Member
    Posted 3 years ago #

    That had not solved the issues. Guys if your code is correct, then to make the website work, simply do this

    1. set your permalinks to default and then delete the .htaccess file
    2. reset the permalinks to whatever you want and then upload the htaccess file back.

  20. Harish Chouhan
    Member
    Posted 3 years ago #

    not helping. When i fixed the pagination, the individual page for the custom post type are not opening and I get a 404 error.

  21. rafaelxy
    Member
    Posted 3 years ago #

    After a looong day debugging thru wordpress core, I managed to solve this issue.

    Basicly, you CANT have a PAGE and a CUSTOM POST TYPE with the same name. If you do, the permalink rewrite rules will get confused and trigger a 404.

    A very simple solution I'm using is: The page that lists the custom post types is called in plural (eg. products) and the actual post type name is in singular (eg. product). So they dont conflict and it's all fine.

    Done Done!
    Hope this will save people's time.

  22. TakeoK
    Member
    Posted 3 years ago #

    @rafaelxy: Wow... that was exactly my problem! Never would have thought of that. Thank you!

  23. rafaelxy
    Member
    Posted 3 years ago #

    @TakeoK:
    glad I'm helping!

    I did a function as well that you can use it instead of the wordpress query_posts(), this function does exactly the same but adding some more stuff that is need to pagination work correctly on custom post types pages:

    http://rafaelxy.pastebin.com/VtHQEkH7

  24. artemisian
    Member
    Posted 3 years ago #

    God bless you for figuring that out. It was driving me crazy!

  25. bigevilbrain
    Member
    Posted 3 years ago #

    @rafaelxy: Thanks a ton! This also worked for me. Perfect.

    I've got two custom post types I'm using: caststudy and partner.

    I switched my page-slug to "case-study" (from "case-studies") and to "partner" (from "partners") and the pagination works!

    "Archive" Pages
    page-case-study.php
    page-partner.php

    Single View
    single-casestudy.php
    single-partner.php

    I also figured out a way to fool wp_list_pages() into highlighting the correct "parent page" when viewing any single custom post-type "page" (rather than highlighting the "blog" page.)

    This is really ugly but it works.

    <?php
    $defaults = array(
    'depth'			=> 0,
    'title_li'		=> '',
    'echo'			=> 1,
    'sort_column'	=> 'menu_order, post_title',
    'sort_order'	=> 'asc',
    'link_before'	=> '',
    'link_after'	=> '');
    
    global $post, $wp_query;
    
    if (get_query_var('post_type') == 'casestudy') {
    // Load up the 'Case Studies' page to fool WP.
    $page = get_page_by_title('Case Studies');
    
    $temp_post = $post; $temp_query = $wp_query; $wp_query = null;
    
    $wp_query = new WP_Query();
    $wp_query->query(array('page_id' => $page->ID));
    wp_list_pages($defaults);
    
    // Restore previous wp_query.
    $wp_query = null; $wp_query = $temp_query; $post = $temp_post;
    } else if (get_query_var('post_type') == 'partner') {
    // Load up the 'Partners' page to fool WP.
    $page = get_page_by_title('Partners');
    
    $temp_post = $post; $temp = $wp_query; $wp_query = null;
    
    $wp_query = new WP_Query();
    $wp_query->query(array('page_id' => $page->ID));
    wp_list_pages($defaults);
    
    // Restore previous wp_query.
    $wp_query = null; $wp_query = $temp_query; $temp_query = $temp_post;
    } else {
    // A normal page.
    wp_list_pages($defaults);
    }
    ?>

    Hope that helps someone.

  26. StevieG
    Member
    Posted 3 years ago #

    @rafaelxy: I do hope this all works I am going completely mad here! Can you post an example of how to use your function please.

  27. cpres
    Member
    Posted 3 years ago #

    To use @rafaelxy's code you just need to copy and paste it to you functions.php file. I just dropped it right on the bottom of mine.

    Then this in your index.php (or what have you):

    <? custom_query_posts()?>
    <? while (have_posts()) : the_post(); ?>

    And it works!

    One thing that took me way too long to figure out was the:
    <?php if (have_posts()) : ?>
    I found when /page/2 came it registers as not having any have_posts and thus always popping up the "No posts found". Frustrating at the very least I assure you.

    So if you're running across that issue and know you have more than a single page of posts take out the if have_posts and you might be happily surprised.

  28. Olegs Belousovs
    Member
    Posted 3 years ago #

    @rafaelxy & @bigevilbrain Thank you so much!!! Became crazy for a half day... all that solutions online don't works. This is the hack! Clap clap!!

  29. marcelotorres
    Member
    Posted 3 years ago #

    @rafaelxy Thank you for sharing this discovery, had long been looking for a solution and i just found \o/

  30. @sefod
    Member
    Posted 3 years ago #

    @rafaelxy ... Thank u, Thank u, Thank u!
    i've registered here only for say that.
    6 hours googling.

Topic Closed

This topic has been closed to new replies.

About this Topic