如何限制通过此 PHP 代码输出的 Wordpress 类别的数量?

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

所以在这个代码示例中,我得到了前端输出的每个 post 的所有类别,在所需的 DIV 中,在循环中。
太棒了

但出于布局目的,我希望能够将其限制为仅从每个 post 输出两个类别,即使它有 5 个类别或 10 个关联。

有什么代码建议吗?

<?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();

您可以在 function.php 或插件中重写 wordpress 功能:

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>

简单的方法是获取类别并使用 php 内置函数 array_slice

这是您可以使用的代码,请参考您的示例代码。

<?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();