WordPress.org

Forums

[resolved] Taxonomy Pagination (21 posts)

  1. alanchrishughes
    Member
    Posted 4 years ago #

    Has anybody ever figured this out yet? You Google it and there are a billion blogs and forums looking for how but no answers or even failed attempts to answer.

  2. MichaelH
    Member
    Posted 4 years ago #

    Might need to expand on what's not working for you.

    Using the TwentyTen theme, with default Permalins, and with Settings->Writing set to 1 post page, when I visit a taxonomy archive (archive.php used from Twenty Ten) pagination works just fine.

  3. alanchrishughes
    Member
    Posted 4 years ago #

    It's hard to narrow it down any more than it just doesn't work.

    Right now my taxonomy template file queries the posts fine using this

    <?php $posts = query_posts($query_string . '&orderby=title&order=asc&posts_per_page=5');
    
    if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    <a>" rel="bookmark">
    <?php the_title(); ?>
    </a>
    
    <?php the_excerpt(); ?>
    
    <?php endwhile; ?>
    
    <?php endif; ?>

    And then I just copy and pasted the pagination code from the TwentyTen loop.php file

    <?php if (  $wp_query->max_num_pages > 1 ) : ?>
    
    <?php next_posts_link( __( '<span class="meta-nav">←</span> Older posts', 'twentyten' ) ); ?>
    <?php previous_posts_link( __( 'Newer posts <span class="meta-nav">→</span>', 'twentyten' ) ); ?>
    
    <?php endif; ?>

    But if I have two pages and click the older posts link, it gives me a 404, if I have 4 pages it will take me back to one older page, but for the 3rd and 4th pages it gives me 404s.

    All of the individual posts are fine, all the links that show up work fine, if I remove the limited posts per page it will display every single post listed under that taxonomy term and link to the posts fine.

  4. MichaelH
    Member
    Posted 4 years ago #

    Example of proper pagination (using the paged query var):

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts('cat=3&posts_per_page='.get_option('posts_per_page').'&paged=' . $paged);
    ?>
  5. alanchrishughes
    Member
    Posted 4 years ago #

    Thanks MichaelH, but that isn't even displaying the links now.

  6. MichaelH
    Member
    Posted 4 years ago #

    Then paste your complete template in a pastebin (e.g. wordpress.pastebin.com), report the link back here and maybe someone can spot the problem.

  7. alanchrishughes
    Member
    Posted 4 years ago #

    This is with what you suggested

    http://wordpress.pastebin.com/qUy09Ac0

    And this is what I was trying and works only if I have four pages and only links back to one previous page

    http://wordpress.pastebin.com/5U8ZmgYF

  8. MichaelH
    Member
    Posted 4 years ago #

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts($query_string .'&posts_per_page=5&paged=' . $paged);
    if (have_posts()) : while (have_posts()) : the_post();
    the_title();
    the_excerpt();
    endwhile;
    ?>
    <div class="navigation">
      <div class="alignleft"><?php next_posts_link('« Older Entries') ?></div>
      <div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div>
    </div>
    
    <?php
    endif;
    get_footer(Credits);
    ?>

    Note: that the only reason you really need a specific template for this is that you are wanting a different number of posts displayed for taxonomies than you specify in your Settings->Reading->Blog pages show x posts.

  9. alanchrishughes
    Member
    Posted 4 years ago #

    Thanks again, unfortunately that is giving me the same error. It will work linking to one previous page, but then anything after that it says it can't be found.

    PS: I have to use a different template because I do a lot of stuff with this site and have a number of templates that display different content in different ways.

  10. alanchrishughes
    Member
    Posted 4 years ago #

    Actually I think this is going to work. I deleted my template to see if it would work with the default archive.php file as it's template, to see if there was a greater problem, and it worked.

    Then I put my template back in (with your recent code), but WordPress wasn't recognizing the template, like it does sometimes, so I did that trick of visiting the permalinks page and then it recognized it, and the links started working.

  11. MichaelH
    Member
    Posted 4 years ago #

    Link to see the problem might be useful.

  12. alanchrishughes
    Member
    Posted 4 years ago #

    It's installed locally right now, I don't want to put anything online until I get everything working. Your code seems to be working now though. Would there be much difference using this to query posts from a custom post type?

    Im currently using this to query the posts but it still needs working pagination also.

    http://wordpress.pastebin.com/C6K00VPm

  13. MichaelH
    Member
    Posted 4 years ago #

    If you are creating your own template to display posts/custom posts you need to use the paged query var and the next/previous posts link code.

    Disable all plugins, switch to the twentyten theme. Update your permalinks to use the Default permalinks. In single.php put this line after the_content() (line 30) and put your custom taxonomy name in place of genre.

    <?php echo get_the_term_list( $post->ID, 'genre', '<p>Genre: ', ', ', '</p>' ); ?>

    Then visit a single post, click on the taxonomy link and see if pagination works.

  14. alanchrishughes
    Member
    Posted 4 years ago #

    Thanks. There is a lot of that I can't do though. I need to keep the same theme, I need to keep the /%postname%/ permalink structure, and I need to keep my single.php posts separate from this section of the site.

    Is that the only way you can use custom posts?

  15. MichaelH
    Member
    Posted 4 years ago #

    Okay, just trying to make sure paging works in your environment for those conditions.

    Maybe someone else will have some other solutions.

  16. alanchrishughes
    Member
    Posted 4 years ago #

    Is that the only way possible to paginate custom posts? If you have to use the same template for all post types that kind of defeats the purpose of even having them.

  17. alanchrishughes
    Member
    Posted 4 years ago #

    What about using the same code you gave me above for taxonomies, but for one specific taxonomy term on specific template? Something on that $query_string line maybe?

    I'm creating a local music site/section on an already existing WordPress site that is only used as a CMS.

    I'm using taxonomy terms for different music genres, which now thanks to your help, I can display page listings of properly.

    But I still need to create a paginated template to display shows. I was thinking of using a whole new custom post type for them, but that probably isn't necessary plus I think you can only narrow a WP search bar down to one post type.

    http://wordpress.org/support/topic/limit-search-results-to-custom-post-type?replies=8

    I could just display them as if they were music genres, but I think the design of a schedule page should be different, and it would still be url'd under musicgenres which would not make sense. So that is why I am looking for its own specific template and url. Any ideas. Thanks again for helping me this far, if you can help me get this last problem out of the way I think I should be good to go.

  18. alanchrishughes
    Member
    Posted 4 years ago #

    I think I'll I would need is how to re-write

    'post_type' => 'the post type's name', 'the taxonomy name' => 'the taxonomy term',

    into the way you wrote it with the ampersands

    query_posts($query_string .'&posts_per_page=5&paged=' . $paged);

  19. alanchrishughes
    Member
    Posted 4 years ago #

    Oh come on man don't leave me hanging here haha. You've helped me out a lot already and I definitely appreciate it, I know you don't have to help me out at all, but my whole site basically is just stuck waiting on this one last glitch.

    I've tried a few things that seem like they should work like

    query_posts($query_string .'&posts_per_page=5&mytaxonomy=mytaxonomyterm&paged=' . $paged);

    but no luck, it just shows a link to the page I am on.

  20. MichaelH
    Member
    Posted 4 years ago #

    Will give this one last shot--this works as a Page Template and shows using both custom post types (e.g. book) and custom taxonomy (e.g. genre=mystery)

    <?php
    /*
    Template Name: CustomTax
    */
    
    get_header(); ?>
    
    <div id="content" class="narrowcolumn">
    
    <?php
      $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
      $post_per_page = 1; // -1 shows all posts
      $do_not_show_stickies = 1; // 0 to show stickies
      $args=array(
        'genre' => 'mystery',
        'post_type' => 'book',
        'paged' => $paged,
        'posts_per_page' => $post_per_page
      );
      $temp = $wp_query;  // assign orginal query to temp variable for later use
      $wp_query = null;
      $wp_query = new WP_Query($args);
      if( have_posts() ) :
    		while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    	    <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
            <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
            <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
            <div class="entry">
              <?php the_content('Read the rest of this entry »'); ?>
            </div>
            <p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments ', '1 Comment ', '% Comments '); ?></p>
          </div>
        <?php endwhile; ?>
        <div class="navigation">
          <div class="alignleft"><?php next_posts_link('< Older Entries') ?></div>
          <div class="alignright"><?php previous_posts_link('Newer Entries >') ?></div>
        </div>
      <?php else : ?>
    
    		<h2 class="center">Not Found</h2>
    		<p class="center">Sorry, but you are looking for something that isn't here.</p>
    		<?php get_search_form(); ?>
    
    	<?php endif; 
    
    	$wp_query = $temp;  //reset back to original query
    
    ?>
    
    	</div>
    
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>

    Also see: http://codex.wordpress.org/Template_Tags/query_posts#Preserving_the_Original_Query_.28Pagination_etc..29

    Good Luck.

  21. alanchrishughes
    Member
    Posted 4 years ago #

    That actually worked! Thank you very much.

    The url's are still kind of screwy but I know there is nothing that can be done about that, its just the way WordPress works right now.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags