• Resolved dkristine

    (@dkristine)


    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 - 16 through 30 (of 44 total)
  • I got the prev/next links to show up using this code:
    https://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 https://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.

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

    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.

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

    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.

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

    @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:

    https://rafaelxy.pastebin.com/VtHQEkH7

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

    @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.

    @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.

    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.

    @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!!

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

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

    Nice work @rafaelxy!

    What is it, in this global variable “$wp_query”, that fixes this issue?

    <?php
    global $wp_query;
    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    $args = array(
    	'post_type' => 'CUSTOM_POST_NAME',
    	'product_category' => 'CATEGORY_NAME',
    	'posts_per_page' => 6,
    	'post_status' => 'publish',
    	'orderby' => 'menu_order',
    	'order' => 'DESC',
    	'paged' => $paged
    );
    $wp_query = new WP_Query($args);
    
    while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
    	<a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a>
    <?php endwhile; ?>

    I am just trying to understand this, that’s all. ??

    Thanks
    Vayu

Viewing 15 replies - 16 through 30 (of 44 total)
  • The topic ‘Pagination with custom post type listing’ is closed to new replies.