WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] get_terms listing and pagination (3 posts)

  1. hellau
    Member
    Posted 1 year ago #

    Hi,

    I would like to paginate a custom taxonomy terms listing.

    Here are some details :
    - Custom taxonomy : scenographie
    - terms (unique) inside : scene.

    Code in my template :

    $taxonomyName = "scenographie";
    	$terms = get_terms($taxonomyName,array(
    		'parent' => 67
    	));
    	foreach($terms as $term) {?>
    
    		<div id="post-<?php the_ID();?>" class="posts">
    
    			<div class="scene-list-thb">
    				<?php $tax_term_id = $term->term_taxonomy_id;
    			 	$images = get_option('taxonomy_image_plugin');?>
    			 	<a href="<?php echo get_term_link($term->slug,$taxonomyName)?>" title="<?php echo $term->name ?>">
    			 		<?php echo wp_get_attachment_image( $images[$tax_term_id], 'scenes-thb' );?>
    			 	</a>
    		 	</div>
    
    		 	<div class="scene-infos <?php if(!has_post_thumbnail()) : ?>full<?php endif; ?>">
    	        	<h2><a href="<?php echo get_term_link($term->slug,$taxonomyName)?>" title="<?php echo $term->name ?>"><?php echo $term->name ?></a></h2>
    	        	<div class="scene-desc">
    	        		<p>
    		        		<?php
    		        		$termDesc = $term->description;
    		        		echo truncate($termDesc, 650, "...", true);
    		        		?>
    	        		</p>
    	        	</div>
    	        	<a class="scene-details" href="<?php echo get_term_link($term->slug,$taxonomyName)?>">Voir le détail de cette scène</a>
            	</div>
    
            </div>
    
    	<?php } ?>

    I'm using a plugin to use image on category / taxonomys.

    So what i'm looking for is display only 3 result per page, and get everything else paginate.

    Is it possible, and how ?

    Thx

  2. vtxyzzy
    Member
    Posted 1 year ago #

    Here is an article that describes the use of the WP paginate_links() function to paginate an array. Use is as an example and adapt to your code: http://wordpress.mcdspot.com/2010/11/25/pagination-using-paginate_links/

  3. hellau
    Member
    Posted 1 year ago #

    Hey !
    Thanks to your advice, i've finally came to a solution !

    Here's what I did :

    $taxonomyName = "scenographie";
    	$terms = get_terms($taxonomyName, 'parent=67');
    
    	function objectToArray($d) {
    	  if (is_object($d)) {
    
    	   $d = get_object_vars($d);
    	  }
    
    	  if (is_array($d)) {
    
    	   return array_map(__FUNCTION__, $d);
    	  }
    	  else {
    	   // Return array
    	   return $d;
    	  }
    	 }
    
    	wp_reset_query();
    
    	$rows_per_page = 3;
    	$current = (intval(get_query_var('paged'))) ? intval(get_query_var('paged')) : 1;
    
    	//echo "current ==> ".$current;
    
    	global $wp_rewrite;
    
    	$pagination_args = array(
    	 'base' => @add_query_arg('paged','%#%'),
    	 'format' => '',
    	 'total' => ceil(count($terms)/$rows_per_page),
    	 'current' => $current,
    	 'show_all' => false,
    	 'type' => 'list',
    	 'prev_next'    => true,
    	 'prev_text'    => __('«'),
    	 'next_text'    => __('»')
    	);
    
    	if( $wp_rewrite->using_permalinks() )
    	 $pagination_args['base'] = user_trailingslashit( trailingslashit( remove_query_arg('s',get_pagenum_link(1) ) ) . 'page/%#%/', 'paged');
    
    	if( !empty($wp_query->query_vars['s']) )
    	$pagination_args['add_args'] = array('s'=>get_query_var('s'));
    
    	$start = ($current - 1) * $rows_per_page;
    	$end = $start + $rows_per_page;
    	$end = (count($terms) < $end) ? count($terms) : $end;
    
    	echo '<br />';
    	for ($i=$start;$i < $end ;++$i ) { ?>
    		<?php $term = $terms[$i] ?>
    
    		<div id="post-<?php the_ID();?>" class="posts">
    
    			<div class="scene-list-thb">
    				<?php $tax_term_id = $term->term_taxonomy_id;
    			 	$images = get_option('taxonomy_image_plugin');?>
    			 	<a href="<?php echo get_term_link($term->slug,$taxonomyName)?>" title="<?php echo $term->name ?>">
    			 		<?php echo wp_get_attachment_image( $images[$tax_term_id], 'scenes-thb' );?>
    			 	</a>
    		 	</div>
    
    		 	<div class="scene-infos <?php if(!has_post_thumbnail()) : ?>full<?php endif; ?>">
    	        	<h2><a href="<?php echo get_term_link($term->slug,$taxonomyName)?>" title="<?php echo $term->name ?>"><?php echo $term->name ?></a></h2>
    	        	<div class="scene-desc">
    	        		<p>
    		        		<?php
    		        		$termDesc = $term->description;
    		        		echo truncate($termDesc, 650, "...", true);
    		        		?>
    	        		</p>
    	        	</div>
    	        	<a class="scene-details" href="<?php echo get_term_link($term->slug,$taxonomyName)?>">Voir le détail de cette scène</a>
            	</div>
    
            </div>
    
    	<?php } ?> 
    
    	<?php
    	echo "<div class='pagination scenes-pagination'>";
    	echo paginate_links($pagination_args);
    	echo "</div>";
    	?>

    I got an none working pagination at the beginning, and i juste add "wp_reset_query()" before that code, and it worked.

    Thx again ;)

Topic Closed

This topic has been closed to new replies.

About this Topic