I have a custom post type with taxonomies, it shows all my taxonomies on one page and I want to limit 10 taxonomies per page how can I do that?
I'm using the following code, does anyone have suggestions or ideas why it's not working? Thanks in advance!
Here is my code:
<?php
$post_type = 'brinton';
$taxonomies = get_object_taxonomies( array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) :
?>
<?php
$args = array(
'post_type' => $post_type,
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $term->slug,
)
)
);
$posts = new WP_Query($args);
if( $posts->have_posts() ):
?>
<div class="cp-product-list-w">
<div class="c-haeding-3">
<h4><?php echo $term->name; ?></h4>
<h2 class="c-brinton-cat"><?php echo $term->description; ?></h2>
</div>
<div class="cp-product-list-in">
<div class="c-container">
<div class="row">
<?php while( $posts->have_posts() ) : $posts->the_post(); ?>
<div class=" col-lg-3 col-md-4">
<div class="cp-product-list-box brintons-list">
<a href="<?php the_permalink(); ?>"></a>
<div class="cp-product-list-box-img">
<?php the_post_thumbnail('medium'); ?>
</div>
<div class="brintons-list-d">
<h3><?php echo get_the_title(); ?></h3>
<?php
$post_id = get_the_ID();
$product_sku = get_field('brinton_product_sku', $post_id);
?>
<p><?php echo $product_sku; ?></p>
</div>
</div>
</div>
<?php endwhile; endif; ?>
</div>
</div>
</div>
</div>
<?php endforeach; endforeach; ?>
My Answer:
To limit the number of taxonomies displayed per page, you can use the `posts_per_page` parameter in your query. Here is an example of how you can modify your code to achieve this:
php
$args = array(
'post_type' => 'your_custom_post_type',
'posts_per_page' => 10, // Limit to 10 taxonomies per page
);
$taxonomies_query = new WP_Query($args);
if ($taxonomies_query->have_posts()) {
while ($taxonomies_query->have_posts()) {
$taxonomies_query->the_post();
// Display your taxonomies here
}
}
wp_reset_postdata();
Make sure to replace `'your_custom_post_type'` with the name of your custom post type. This code will limit the number of taxonomies displayed per page to 10.
Rate this post
3 of 5 based on 3132 votesComments