kjettamoen
kjettamoen

Reputation: 25

How to limit the number of Wordpress categories outputted through this PHP code?

So in this code example I am getting all the categories of each post outputted on the frontend, inside the desired DIV, down there in The Loop.
Which is great.

But for layout purposes I want to be able to limit this to only outputting TWO categories from each post, even if it has 5 or 10 associated.

Any code suggestions?

<?php
// The Query
$args = array(
    'post_type' => 'post',
    'posts_per_page' => 36,
    'orderby' => 'date',
    'order' => 'DESC',
    'tax_query' => array(
        array(
            'taxonomy' => 'menuelement',
            'field'    => 'slug',
            'terms'    => array('news', 'debate', 'depth'),
        ),
    ),
);
$menuelements = new WP_Query( $args );


// checks if has posts
if ($menuelements->have_posts()) :  ?>

<div class="kmn-posts-row">
<?php
    // The Loop
    while ($menuelements->have_posts()) : $menuelements->the_post(); ?>

             <div class="kmn-posts-category">   
             <span><?php echo get_modified_term_list( get_the_ID(), 'category', '', '', '', array(1)); ?></span>
             </div>

<?php 
endwhile;
?>
</div>
<?php

else :
    echo 'No posts in query.';

endif;

/* Restore original Post Data 
 * NB: Because we are using new WP_Query we aren't stomping on the 
 * original $wp_query and it does not need to be reset with 
 * wp_reset_query(). We just need to set the post data back up with
 * wp_reset_postdata().
 */
wp_reset_postdata();

Upvotes: 1

Views: 362

Answers (2)

Dmitry Leiko
Dmitry Leiko

Reputation: 4372

You can rewrite worpress function in your function.php or plugin:

define( 'PRODUCT_CATEGORY_LIMIT', 2 );
function custom_get_modified_term_list( $id = 0, $taxonomy, $before = '', $sep = '', $after = '', $exclude = array() ){
    $terms = get_the_terms($id, $taxonomy);

    $term_links = [];

    if (is_wp_error($terms)) {
        return $terms;
    }

    if (empty($terms)) {
        return false;
    }

    foreach ($terms as $term) {

        if (!in_array($term->term_id, $exclude)) {
            $link = get_term_link($term, $taxonomy);
            if (is_wp_error($link)) {
                return $link;
            }
            if ( count($term_links) === PRODUCT_CATEGORY_LIMIT ) {
                break;
            }
            $term_links[] = '<a href="'.$link.'" rel="tag">'.$term->name.'</a>';
        }
    }

    if (!isset($term_links)) {
        return false;
    }

    return $before.join($sep, $term_links).$after;
}


<div class="kmn-posts-category">
     <span><?php echo custom_get_modified_term_list( get_the_ID(), 'category', '', '', '', array(1)); ?></span>
</div>

Upvotes: 0

Ali Qorbani
Ali Qorbani

Reputation: 1263

the simple way is to get categories and use php build-in function array_slice

here is the code you can use refer to your sample code.

<?php
// The Query
$args = array(
    'post_type' => 'post',
    'posts_per_page' => 36,
    'orderby' => 'date',
    'order' => 'DESC',
    'tax_query' => array(
        array(
            'taxonomy' => 'menuelement',
            'field'    => 'slug',
            'terms'    => array('news', 'debate', 'depth'),
        ),
    ),
);
$menuelements = new WP_Query( $args );


// checks if has posts
if ($menuelements->have_posts()) :  ?>

<div class="kmn-posts-row">
<?php
    // The Loop
    while ($menuelements->have_posts()) : $menuelements->the_post(); ?>

             <div class="kmn-posts-category">   
             <span><?php 
    $categories = get_the_category();
    array_slice($categories,0,2);
    foreach($categories as $cat){
        echo '<a href="'.get_category_link($cat).'">'.$cat->name.'</a>';
    }
     ?></span>
             </div>

<?php 
endwhile;
?>
</div>
<?php

else :
    echo 'No posts in query.';

endif;

/* Restore original Post Data 
 * NB: Because we are using new WP_Query we aren't stomping on the 
 * original $wp_query and it does not need to be reset with 
 * wp_reset_query(). We just need to set the post data back up with
 * wp_reset_postdata().
 */
wp_reset_postdata();

Upvotes: 2

Related Questions