WordPress.org

Ready to get started?Download WordPress

Forums

How to paginate custom taxonomy terms (23 posts)

  1. theendtimes
    Member
    Posted 1 year ago #

    I have a system that displays Taxonomy terms as "Series" and within those Series are custom post types called "Sermons".

    I have reached the point where there are 12 series to date and would love to paginate these Series.

    I already have pagination script and it works for posts and custom post types but I can't get it to work with my Series (again, custom taxonomy terms).

    The page that displays the "Series Archive" which is where I want the pagination to take place currently displays 12 Series. Ideally I would like it to display 9 and then paginate the next set of Series. Here is the link to the series archive

    If anyone could help, I would GREATLY appreciate it!!

    Thanks in advance!

    P.S The code currently used to display the series as they are now is as follows:

    <div id="recentseries">
    
    <?php
    // Setup the arguments to pass in
    $args = array(
    
            'order'        => 'ASC',
            'orderby'      => 'menu_order',
    		'offset'       => 1,
    );
    
    // Gather the series
    $series = get_terms( 'series', $args );
    
    // Loop through and display the series
    foreach($series as $s)
    {
            $theurl = get_term_link($s, 'series');
            echo "<div class=\"ser-img img\" ><a href=\"" . $theurl . $append . "\">". $taxonomy_images_plugin->get_image_html('large', $s->term_taxonomy_id) ."</a>";
            echo "<div class=\"howmanysermons\">$s->count </div><div class=\"seriesdesc\" >". $s->description . "</div></div>";
    }
    ?>
    
    </div><!-- recentseries -->
    
     <?php if (function_exists("pagination")) {
        pagination($additional_loop->max_num_pages);
    } ?> 
    
    </div>
  2. Jason (Theme Blvd)
    Member
    Posted 1 year ago #

    I'm not quite sure what you're trying to do will be possible with the default elements of WordPress or any generic WP pagination script you find.

    WordPress's pagination is all centered around a primary query of posts. A taxonomy by definition is a way of classifying or categorizing posts. I'm personally not aware of any way to actually paginate the listing of the taxonomy terms. I think you'd have to come up with something custom.

  3. You need to modify the offset parameter to match the current page you are on.

    This should get pretty close:

    <div id="recentseries">
    
    <?php
    
    if ( get_query_var( 'paged' ) )
    	$paged = get_query_var('paged');
    else if ( get_query_var( 'page' ) )
    	$paged = get_query_var( 'page' );
    else
    	$paged = 1;
    
    $per_page    = 20;
    $total_terms = 12; // This needs to be the total number of series (count the number of terms in the series with a function)
    $offset      = $per_page * ( $paged - 1) ;
    
    // Setup the arguments to pass in
    $args = array(
            'order'        => 'ASC',
            'orderby'      => 'menu_order',
    		'offset'       => $offset,
    );
    
    // Gather the series
    $series = get_terms( 'series', $args );
    
    // Loop through and display the series
    foreach($series as $s)
    {
            $theurl = get_term_link($s, 'series');
            echo "<div class=\"ser-img img\" ><a href=\"" . $theurl . $append . "\">". $taxonomy_images_plugin->get_image_html('large', $s->term_taxonomy_id) ."</a>";
            echo "<div class=\"howmanysermons\">$s->count </div><div class=\"seriesdesc\" >". $s->description . "</div></div>";
    }
    ?>
    </div><!-- recentseries -->
    
     <?php if (function_exists("pagination")) {
    	echo paginate_links( array(
    		'base'    => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    		'format'  => '?paged=%#%',
    		'current' => $paged,
    		'total'   => ceil( $total_terms / $per_page ) // 20 items per page
    	) );
    } ?>
    
    </div>
  4. Jason (Theme Blvd)
    Member
    Posted 1 year ago #

    @Pippin Very cool. Seems strange to almost "force" pagination like that. I'm wondering if by doing that WordPress will not understand the pagination happening and give a 404 when you click past the first page. My intial thought is that it would work only if permalinks were turned off. So, I'm interested to see what happens.

    @theendtimes Let us know if it works!

  5. theendtimes
    Member
    Posted 1 year ago #

    A big thanks to the both of you!
    Yes sir will try this out now.
    Thanks again! very grateful.
    I will let you guys know

  6. Brian Dainis (Curotec)
    Member
    Posted 1 year ago #

    Another idea I just had is maybe you could pass the taxonomies to a foreach loop then iterate them out to lets say 100, if you want 10 pages of 10. Then use jQuery to separate them into 10 separate views to the user.

    That way your not forcing WordPress to do something wild and you can still make it seamless to the user.

    The user may prefer that anyway because then there is no lag between switching pages.

  7. wpcanyon
    Member
    Posted 1 year ago #

    Maybe change the format as well, it might also mess with the pagination of the posts query if there's one on the same page.

    So instead of "paged" maybe use "seriespage" or "spage" or something.

  8. Forcing the pagination is not an issue (it's how the codex says to do it). I use it like that quite a bit.

  9. theendtimes
    Member
    Posted 1 year ago #

    Alright So I have tried the method Pippin suggested but had no luck. Also I think I failed to mention that the "Series" custom taxonomy are ongoing so there has to be a way to paginate without specifying a set number of series which I think pippins code was suggesting. Thanks so much for the suggestion though pippin. I really like the idea curotec brought but I am far to beginner to figure out how to put code like that together myself.. if at all possible, how would that look? and thank you wpcanyon and james for all of your help.

  10. theendtimes
    Member
    Posted 1 year ago #

    @Pippin is there a way to grab the total number of terms without specifying a specific number, as the number of terms vary between each series. the only way to get your code to work is if I add " 'number' => 6 " to the array but then it only shows 3 terms on the first page and 3 terms on the second page even though there are more than those 6. I hope I make sense and forgive me for my noob skill level.

  11. Brian Dainis (Curotec)
    Member
    Posted 1 year ago #

    You could use something like http://bxslider.com/ to do the jquery part.
    Its really simple, look at their examples on there.

    Also, check this out: http://codex.wordpress.org/Function_Reference/get_taxonomies

    For the PHP part, maybe something like this:

    <?php
    $taxonomies=get_taxonomies('','names');
    foreach ($taxonomies as $taxonomy ) {
      echo '<a href="#">'. $taxonomy. '</a><br><br>';
    }
    ?>

    Then I'm thinking if you somehow put in a loop counter to add in a div every 10 items. So the formatted HTML looks like this:

    <div>
    <a href="#">Taxonomy</a><br><br>
    <a href="#">Taxonomy</a><br><br>
    <a href="#">Taxonomy</a><br><br>
    <a href="#">Taxonomy</a><br><br>
    <a href="#">Taxonomy</a><br><br>
    <a href="#">Taxonomy</a><br><br>
    <a href="#">Taxonomy</a><br><br>
    <a href="#">Taxonomy</a><br><br>
    <a href="#">Taxonomy</a><br><br>
    <a href="#">Taxonomy</a><br><br>
    </div>
    <div>
    <a href="#">Taxonomy</a><br><br>
    <a href="#">Taxonomy</a><br><br>
    <a href="#">Taxonomy</a><br><br>
    <a href="#">Taxonomy</a><br><br>
    <a href="#">Taxonomy</a><br><br>
    <a href="#">Taxonomy</a><br><br>
    <a href="#">Taxonomy</a><br><br>
    <a href="#">Taxonomy</a><br><br>
    <a href="#">Taxonomy</a><br><br>
    <a href="#">Taxonomy</a><br><br>
    </div>
    <div>
    <a href="#">Taxonomy</a><br><br>
    <a href="#">Taxonomy</a><br><br>
    <a href="#">Taxonomy</a><br><br>
    <a href="#">Taxonomy</a><br><br>
    <a href="#">Taxonomy</a><br><br>
    <a href="#">Taxonomy</a><br><br>
    <a href="#">Taxonomy</a><br><br>
    <a href="#">Taxonomy</a><br><br>
    <a href="#">Taxonomy</a><br><br>
    <a href="#">Taxonomy</a><br><br>
    </div>
    [...]

    You could then use bxslider to page through each div 1 at a time. Fading and vertical or horizontal animations are built in to the script.

    As for the loop counter part to add the div... Ive done it before, but I forget what i did off the top of my head.

  12. Brian Dainis (Curotec)
    Member
    Posted 1 year ago #

    @Pippin is there a way to grab the total number of terms without specifying a specific number, as the number of terms vary between each series. the only way to get your code to work is if I add " 'number' => 6 " to the array but then it only shows 3 terms on the first page and 3 terms on the second page even though there are more than those 6. I hope I make sense and forgive me for my noob skill level.

    You could run a loop before you run your loop that displays the total number of items.

    The first loop would count how many items are available and pass that number to a variable, then your second loop prints the HTML code out foreach item, but uses the variable you established in the first loop to determine how many to print.

  13. theendtimes
    Member
    Posted 1 year ago #

    Thank you brian let me experiment and see what I can come up with in my limited skill level. I will report back when i get warmer/come to a conclusion! you've been a great help!

  14. Brian Dainis (Curotec)
    Member
    Posted 1 year ago #

    No problem!!! If you get stuck on something I'd be glad to help you debug it. Im subscribed to this feed, so Ill see more comments as they come in.

  15. theendtimes
    Member
    Posted 1 year ago #

    Aw great thanks so much!

  16. It'd be better to use an SQL query to get the term count instead of a loop (much more efficient). I don't have the necessary query handy at the moment, but some google searching might bring it up for you.

  17. wpcanyon
    Member
    Posted 1 year ago #

    @Pippin is there a way to grab the total number of terms without specifying a specific number, as the number of terms vary between each series. the only way to get your code to work is if I add " 'number' => 6 " to the array but then it only shows 3 terms on the first page and 3 terms on the second page even though there are more than those 6. I hope I make sense and forgive me for my noob skill level.

    This should work.

    $number_of_series = count( get_terms( 'series' ) );

  18. theendtimes
    Member
    Posted 1 year ago #

    Hey @wpcanyon, That definitely did the job for grabbing the total number of series in a dynamic way, so the pagination navigation(numbers) are working at the bottom of the page but all of my current series (12 so far) are still being displayed on the first page regardless. and when another page is selected within the pagination navigation it does nothing but reload. I have a feeling it may be an easy fix from here (but what do i know)... I have posted my current/updated code.

    <?php
    /**
     * Template Name: Series Archive Page
     */
    
    get_header(); ?>
    
    <div id="body">
    
    <div id="pagecontentfull">
    <div class="hhold"><h6>Series</h6></div>
    
    <div id="recentseries">
    <?php
    
    if ( get_query_var( 'paged' ) )
    	$paged = get_query_var('paged');
    else if ( get_query_var( 'page' ) )
    	$paged = get_query_var( 'page' );
    else
    	$paged = 1;
    
    $per_page    = 3;
    $number_of_series = count( get_terms( 'series' ) );
    $offset      = $per_page * ( $paged - 1) ;
    
    // Setup the arguments to pass in
    $args = array(
            'order'        => 'ASC',
            'orderby'      => 'menu_order',
    		'offset'       => $offset,
    );
    
    // Gather the series
    $series = get_terms( 'series', $args );
    
    // Loop through and display the series
    foreach($series as $s)
    {
            $theurl = get_term_link($s, 'series');
            echo "<div class=\"ser-img img\" ><a href=\"" . $theurl . $append . "\">". $taxonomy_images_plugin->get_image_html('large', $s->term_taxonomy_id) ."</a>";
            echo "<div class=\"howmanysermons\">$s->count </div><div class=\"seriesdesc\" >". $s->description . "</div></div>";
    }
    ?>
    
     <?php if (function_exists("pagination")) {
    	echo paginate_links( array(
    		'base'    => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    		'format'  => '?paged=%#%',
    		'current' => $paged,
    		'total'   => ceil( $number_of_series / $per_page ) // 3 items per page
    	) );
    } ?>
    </div><!-- recentseries -->
    
    </div><!-- pagecontentfull -->
    
    </div> <!-- end of body -->
    
    <?php get_footer(); ?>
  19. Is the page count correct?

  20. theendtimes
    Member
    Posted 1 year ago #

    yes sir, since I have 12 series that are currently being (or supposed to be) broken down 3 per page...

    please feel free to take a look here

  21. wpcanyon
    Member
    Posted 1 year ago #

    Add "number" to the arguments. It's to limit the amount of posts fetched.

    // Setup the arguments to pass in
    $args = array(
            'order'        => 'ASC',
            'orderby'      => 'menu_order',
    	'offset'       => $offset,
    	'number'       => $per_page
    );
  22. theendtimes
    Member
    Posted 1 year ago #

    Aw @ wpcanyon that defnitely makes sense, thank you!

    Almost got it, Everything seems to be working except the navigation to the pagination. If I travel to the pages manually (by appending "page/2/" to the url) you can see that the pagination is working.. but the navigation isn't.

    I know I am literally almost done here and you all have helped me greatly to get to this point. What am I missing that will allow the page navigation to work?

  23. theendtimes
    Member
    Posted 1 year ago #

    I got it!!! scratch my last reply. the string - $big was not specified. now it works like a charm! thank you all so much! and Thank you Jason for summoning in your knowledgeable friends!

Topic Closed

This topic has been closed to new replies.

About this Topic