WordPress.org

Ready to get started?Download WordPress

Forums

Custom post type taxonomy pagination (14 posts)

  1. Justin Carroll
    Member
    Posted 3 years ago #

    I have a custom post type called "portfolio" with a custom taxonomy called "portfolio-category". Here's the taxonomy-portfolio-category.php code:

    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
    if (have_posts()) : while ( have_posts() ) : the_post();
    	// output
    endwhile; endif;

    The problem is this page is bound by Settings > Reading > Blog pages show at most 5. Five works good for the style of my blog index, but for the style of my portfolio pages I need more like 24 per page. How do I override what's in my Settings for these custom taxonomy page templates? Hope someone can help. Thanks!

  2. Justin Carroll
    Member
    Posted 3 years ago #

    Since no one is even attempting to answer this I'll continue to add more information for those with the exact same problem. Ok, this will be a bit long-winded, but it will provide the best possible knowledge to troubleshoot the problem. All the information in my original post is still true. I’m using the WP PageNavi plugin for pagination. This particular problem in not getting the taxonomy-portflio-category.php page to paginate is also a problem when WP PageNavi is turned off.

    I’ve had a heck of a time getting pagination to work on the homepage and on a page template page, but I did get them to work. Here’s their code:

    page-home.php (used as a Page template on a static front page called “Home”)

    $paged = 1;
    if ( get_query_var('paged') ) $paged = get_query_var('paged');
    if ( get_query_var('page') ) $paged = get_query_var('page');
    $i = 0;
    $loop = new WP_Query( array( 'post_type' => 'portfolio', 'paged' => $paged, 'posts_per_page' => 24 ) );
    while ( $loop->have_posts() ) : $loop->the_post();
    // output
    $i++; endwhile;
    if ( function_exists( 'wp_pagenavi' ) ) {
        wp_pagenavi( array( 'query' => $loop ) );
        wp_reset_postdata();
    }

    Pagination works!

    page-portfolio.php (used as a Page template on a Page called “Work”)

    $i = 0;
    $loop = new WP_Query( array( 'post_type' => 'portfolio', 'paged' => get_query_var( 'paged' ), 'posts_per_page' => 24 ) );
    while ( $loop->have_posts() ) : $loop->the_post();
    // output
    $i++; endwhile;
    if ( function_exists( 'wp_pagenavi' ) ) {
        wp_pagenavi( array( 'query' => $loop ) );
        wp_reset_postdata();
    }

    Pagination works!

    taxonomy-portfolio-category.php (used as a way to display portfolio sections e.g. print, photography, etc.)

    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
    global $wp_query;
    query_posts( array_merge( $wp_query->query, array( 'posts_per_page' => 2 ) ) );
    if (have_posts()) : while ( have_posts() ) : the_post();
    // output
    endwhile; endif;
    if ( function_exists( 'wp_pagenavi' ) ) {
        wp_pagenavi();
    }

    Page 1 (/portfolio/interactive/) looks great! It’s definitely only posting 2 items and it calculates the correct number of pagination pages. But when you click on page 2 or 3 or 4 the site defaults to index.php and shows “Page not found”. Pagination fails!

    Hopefully I can resolve this soon. I’ve seen A LOT of people with this same problem of pagination on custom taxonomy pages, but no solid solutions. Please help!

  3. Justin Carroll
    Member
    Posted 3 years ago #

    I’ve made some headway! There’s still an issue, but here’s the code that currently sort of works …

    function my_modify_posts_per_page() {
        add_filter( 'option_posts_per_page', 'my_option_posts_per_page' );
    }
    function my_option_posts_per_page( $value ) {
        return 2;
    }
    add_action( 'init', 'my_modify_posts_per_page', 0);

    The problem is this makes ALL loops return only 2 posts per page. Right now I’m working on getting a conditional statement in there that only checks for taxonomy “portfolio-category”. I’ll post again when I’ve finally figured this out.

  4. Justin Carroll
    Member
    Posted 3 years ago #

    Ok, I’ve done it. There must be a more graceful way to do this, but I’ve put this code in my functions.php file and it works …

    $option_posts_per_page = get_option( 'posts_per_page' );
    add_action( 'init', 'my_modify_posts_per_page', 0);
    function my_modify_posts_per_page() {
        add_filter( 'option_posts_per_page', 'my_option_posts_per_page' );
    }
    function my_option_posts_per_page( $value ) {
        global $option_posts_per_page;
        if ( is_tax( 'portfolio-category') ) {
            return 2;
        } else {
            return $option_posts_per_page;
        }
    }

    If anyone knows a better way to write this code please hook me up! Thanks.

  5. JoE
    Member
    Posted 3 years ago #

    Thanks for posting this up! Saw your post on StackOverflow.com too and couldn't believe nobody has provided a solid solution to this yet. Plopped your adjustment to the functions.php file into mine, changed the taxonomy and it works like a charm.

    So I know it took a lot of work on your part, without much feedback from the WP Community, but thank you!

  6. dondmcg
    Member
    Posted 3 years ago #

    J,
    I am desperately trying what you have accomplished here. I have gotten to the point that you have just before you effect the functions file. But when I add your code to the functions.php it bombs out and the site goes to the white screen.

    I am using Elegant Themes but this functions.php edit should be above that correct?

    I am editing the functions.php file in wp-includes and when either of these lines are un-commented the site bombs:

    $option_posts_per_page = get_option( 'posts_per_page' );
    add_action( 'init', 'my_modify_posts_per_page', 0);

    Elegant Themes uses:
    get_option('wooden_archivenum_posts');
    but I tried using this in place of your call for get_options() and it did not work. Unfortunately Elegant Themes has not gotten back to me. Do you think it is a theme conflict?
    Here is my example:
    http://www.marygiuliani.com/publisher/BIZBASH (taxonomy-publisher.php)
    parent page:
    http://www.marygiuliani.com/press (page-presspiece.php)

  7. Justin Carroll
    Member
    Posted 3 years ago #

    Elegant Themes could be using a framework that lends itself to what they're doing with their pagination. So, sorry, I'm not sure.

    But since writing on this post I have begun to rely on WordPress built-in pagination which I didn't even know existed. It's quite reliable. Read bout it here (The Underused WordPress Pagination Function) ... http://coding.smashingmagazine.com/2011/05/10/new-wordpress-power-tips-for-template-developers-and-consultants/

    Also, when I'm using custom taxonomies I do a lot of encapsulation and this helps keep the pagination correct. So for instance I will use archive-customposttype.php for all taxonomy pages. In taxonomy-customwhatever.php I'll simply do a include(archive-customposttype.php) and that seems to keep things straight. But I do realize sometimes that doesn't work because a developer sometimes needs to customize those pages. Hope that helps!

  8. tradiart
    Member
    Posted 3 years ago #

    Thanks Justin for answering my email. Also find this useful post and applied your solution, seems to work fine.

    You're a magician! :-)

  9. jeroenpanjer
    Member
    Posted 3 years ago #

    I was struggling with pagination for hours as well.. This is what I did that worked:

    <?php $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; ?>
    
    <?php $offset = ( 5 * $paged ) - 5; ?>
    <?php $args=array(‘paged’=>$paged, 'posts_per_page'=>5, 'post_type'=>'YOURPOSTTYPE', 'offset' => $offset); ?>
    <?php query_posts($args); ?>
  10. Robin John
    Member
    Posted 2 years ago #

    @jeroenpanjer Thanx for tat bit^^

  11. kahfooi
    Member
    Posted 2 years ago #

    $option_posts_per_page = get_option( 'posts_per_page' );
    add_action( 'init', 'my_modify_posts_per_page', 0);
    function my_modify_posts_per_page() {
        add_filter( 'option_posts_per_page', 'my_option_posts_per_page' );
    }
    function my_option_posts_per_page( $value ) {
        global $option_posts_per_page;
        if ( is_tax( 'portfolio-category') ) {
            return 2;
        } else {
            return $option_posts_per_page;
        }
    }

    According to this code, what if i have 2 different taxonomies for 1 custom post type? Where should i add another taxonomy?

  12. nackle2k10
    Member
    Posted 2 years ago #

    Great method, I also saw your chaining method post in wptuts. but I have a problem using this method when use custom permalinks, still showing 404 error page :( and I've been stuck with this for 2 days. Please somebody help me...

  13. Justin Carroll
    Member
    Posted 2 years ago #

    Nackle, you must have some other code either hand-written or plugin code that's interfering with the pagination. Simply download the demo files from my wptuts article and they will work out of the box. If they don't work on your project then your other code is likely the issue. Hope that helps.

  14. billseymour
    Member
    Posted 2 years ago #

    @Justin Carroll-
    Nicely done, Justin. Your 4th post in this thread works perfectly for me. I currently am using a simpler If test, whether the page is a taxonomy page (so my If test is if ( is_tax() )...).

    Good work, thanks for sharing your results.

Topic Closed

This topic has been closed to new replies.

About this Topic