如何将内爆函数合并到此列表中

How to incorporate an implode function into this list

这是一个创建 post "names" 列表的 WordPress 函数,但我不确定在每个术语名称之间引入逗号的最优雅方式是什么 - 我想是会涉及内爆和数组,但我对 PHP 真的很陌生,可以在这里使用一些指导。

$terms = get_the_terms( get_the_ID(), 'content' ); // “content” is your custom taxonomy name
if ( $terms && ! is_wp_error( $terms ) ) :    
    foreach ( $terms as $term ) {
        echo '<div class="svg-icon-container" title="'.$term->description.'">'.$term->name.'</div>';
    }
endif;

编辑: 对于那些想要查看条款列表的人 - 很抱歉,我无法在我的网站上实施您的 var_dump 代码目前的 WP 站点,但它看起来像这样:

first impression, for advanced-level players, for beginner-level players, gameplay highlights, gamer affairs, gaming guide, how-to, introduction, review, spotlight, tips, troubleshooting, video production and streaming, walkthrough ...

有几种方法可以做到。

使用implode()

$terms_out = [];
$terms = get_the_terms( get_the_ID(), 'content' ); // “content” is your custom taxonomy name
if ( $terms && ! is_wp_error( $terms ) ) :    
    foreach ( $terms as $term ) {
        $terms_out[] = '<div class="svg-icon-container" title="'.$term->description.'">'.$term->name.'</div>';
    }
endif;
echo implode(', ', $terms_out);

或使用ob_start(), ob_end_clean() and rtrim()

<?php
ob_start();
$terms = get_the_terms( get_the_ID(), 'content' ); // “content” is your custom taxonomy name
if ( $terms && ! is_wp_error( $terms ) ) :    
    foreach ( $terms as $term ) {
        echo '<div class="svg-icon-container" title="'.$term->description.'">'.$term->name.'</div>, ';
    }
endif;
echo rtrim(ob_end_clean(), ', ');

您必须先构建列表

$terms = get_the_terms( get_the_ID(), 'content' ); // “content” is your custom taxonomy name
if ( $terms && ! is_wp_error( $terms ) ){ 
    $list = [];   
    foreach ( $terms as $term ) {
        $list[$term->name] = $term->name; //add as both key and value to make it unique
    }
    //echo some opening html
    echo implode(', ', $list);
   //echo some closing html
}

如果你想 HTML 围绕每个项目,这会令人困惑,如果是这样,请将其包含在数组中,然后将其内爆

$terms = get_the_terms( get_the_ID(), 'content' ); // “content” is your custom taxonomy name
if ( $terms && ! is_wp_error( $terms ) ){ 
    $list = [];   
    foreach ( $terms as $term ) {
        $list[$term->name] = '<div class="svg-icon-container" title="'.$term->description.'">'.$term->name.'</div>'; 
    }

    echo '<div id="my_list" >'.implode(', ', $list).'</div>';
}

HTML 是一个小细节。但在这种情况下确实没有优雅的方法,因为您必须访问名称。

如果实在不想用循环,可以放弃题目,那么可以把cast项object转成数组,然后用array_column()我想。

像这样:

$terms = get_the_terms( get_the_ID(), 'content' ); // “content” is your custom taxonomy name
if ( $terms && ! is_wp_error( $terms ) ){ 
     $t = (array)$terms;  //cast the object $terms to an array.
     $list = array_column('name', $list); 

     echo implode(', ', $list);
}

但是正如我提到的,您无法将描述作为标题。也就是说,我们仍然可以像这样通过内爆来包装每个项目:

 echo '<div class="svg-icon-container" >'.implode('</div>, <div class="svg-icon-container" >', $list).'</div>';

但它变得凌乱且有点重复。我们可以稍微清理一下,我只是想展示 "raw" 的方式。这种方式更好,因为如果你改变 class 你只需要在一个地方改变它。他们靠得很近,但这是一个容易犯的错误。

 $before = '<div class="svg-icon-container" >';
 $after = '</div>';
 echo $before .implode($after.', '.$before, $list).$after;

供参考

Type casting in PHP works much as it does in C: the name of the desired type is written in parentheses before the variable which is to be cast.

http://php.net/manual/en/language.types.type-juggling.php

array_column() returns the values from a single column of the input, identified by the column_key. Optionally, an index_key may be provided to index the values in the returned array by the values from the index_key column of the input array.

http://php.net/manual/en/function.array-column.php

干杯。