简体   繁体   English

Wordpress中自定义分类术语之间的间隔

[英]Space between custom taxonomy terms in wordpress

I am using the following to list the terms within my custom taxonomy 我正在使用以下内容列出我的自定义分类法中的术语

<?php $application_terms = wp_get_object_terms($post->ID, 'application');
  if(!empty($application_terms)){
    if(!is_wp_error( $application_terms )){

      foreach($application_terms as $application){
        echo $application->slug; 
      }
    }
  } 
?>

The code works fine and it displays the terms however, if a post type has two "application" terms assigned to it, the two terms appear without space in betweeen them eg ig apple and banana it appears applebanana. 该代码可以正常工作,并显示术语,但是,如果为帖子类型分配了两个“应用程序”术语,则这两个术语之间会出现空格,例如ig apple和banana,它看起来像applebanana。 How do you change the code so that there is space in between the terms? 您如何更改代码,以便在条款之间留出空间?

Thanks 谢谢

Create an array of slugs and implode it with a blank space 创建一个子弹数组,并用空格填充它

$application_terms = wp_get_object_terms($post->ID, 'application');
if ( $application_terms && !is_wp_error( $application_terms ) {
    $slugs = wp_list_pluck( $application_terms, 'slug' );
    $string = implode( ' ', $slugs );
    echo $string;
}

It should work in the way, you want it to be 它应该以您希望的方式工作

<?php $application_terms = wp_get_object_terms($post->ID, 'application');
  if(!empty($application_terms)){
    if(!is_wp_error( $application_terms )){
      $i = 0;
      foreach($application_terms as $application){
         if($i>0) 
         echo ', ';
         echo $application->slug;
         $i++; 
      }
    }
  } 
?>

so if a post have two category assigned, it will display like apple, banana 因此,如果帖子分配了两个类别,则其显示方式将类似于苹果,香蕉

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM