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

Viewing 13 replies - 1 through 13 (of 13 total)
  • Thread Starter Justin Carroll

    (@3circlestudio)

    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!

    Thread Starter Justin Carroll

    (@3circlestudio)

    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.

    Thread Starter Justin Carroll

    (@3circlestudio)

    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.

    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!

    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:
    https://www.marygiuliani.com/publisher/BIZBASH (taxonomy-publisher.php)
    parent page:
    https://www.marygiuliani.com/press (page-presspiece.php)

    Thread Starter Justin Carroll

    (@3circlestudio)

    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) … https://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!

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

    You’re a magician! ??

    jeroen

    (@jeroenpanjer)

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

    @jeroenpanjer Thanx for tat bit^^

    kahfooi

    (@kahfooi)

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

    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…

    Thread Starter Justin Carroll

    (@3circlestudio)

    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.

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

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Custom post type taxonomy pagination’ is closed to new replies.