【问题标题】:How to limit the number of Wordpress categories outputted through this PHP code?如何限制通过此 PHP 代码输出的 Wordpress 类别的数量?
【发布时间】:2020-01-25 12:16:53
【问题描述】:

因此,在此代码示例中,我将每个帖子的所有类别输出到前端,在所需的 DIV 内,在 The Loop 的下方。
这很棒

但出于布局目的,我希望能够将其限制为仅从每个帖子输出两个类别,即使它有 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();

【问题讨论】:

    标签: php wordpress categories


    【解决方案1】:

    简单的方法是获取类别并使用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();
    

    【讨论】:

      【解决方案2】:

      你可以在你的function.php或插件中重写worpress函数:

      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>
      

      【讨论】:

      • 你的意思是重写那个函数,这样它就不会在 FOREACH 中永远持续下去,而是只适用于 2 或 3 或 4?我喜欢多少?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-12
      • 2015-07-11
      • 2018-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多