Я пытаюсь получить имена терминов таксономии, разделенные запятой. Это код, но он разделяет таксономии и термины с помощью : Если терминов много, они разделяются с помощью : вместо , Если я удаляю символ : между именами терминов таксономии ничего нет.
<ul class='apt-product-terms'>
<?php
$_taxonomies = array(
'taxname1' => __( 'Taxname1', 'textstringdomain' ),
'taxname2' => __( 'Taxname2', 'textstringdomain' ),
'taxname3' => __( 'Taxname3', 'textstringdomain' ),
'taxname4' => __( 'Taxname4', 'textstringdomain' )
);
foreach ($_taxonomies as $taxonomy_slug => $taxonomy_name) {
$terms = get_the_terms( $post->ID, $taxonomy_slug);
if (is_array($terms) && count($terms) > 0) { ?>
<li class='apt-tax-item'>
<span class='apt-term-name'><?php echo $taxonomy_name ?></span>
<ul class='apt-tax-term-list'>
<?php foreach ( $terms as $term ) { ?>
<li class='apt-term-item'>:
<a class='apt-term-link' href = "<?php echo get_term_link($term); ?>"> <?php echo $term->name ?></a>
</li>
<?php } ?>
</ul>
</li>
<?php
}
}
?>
</ul>
В чем проблема. Я думаю, это может быть связано с echo $term->name
🤔 А знаете ли вы, что...
PHP позволяет создавать и отправлять электронные письма с использованием функции mail().
Благодаря примеру @Крис Хаас здесь https://3v4l.org/oZ7JA я изменил свой код таким образом, и теперь он работает так, как я ожидал.
<ul class='apt-product-terms'>
<?php
$_taxonomies = array(
'taxname1' => __( 'Taxname1', 'textstringdomain' ),
'taxname2' => __( 'Taxname2', 'textstringdomain' ),
'taxname3' => __( 'Taxname3', 'textstringdomain' ),
'taxname4' => __( 'Taxname4', 'textstringdomain' )
);
foreach ($_taxonomies as $taxonomy_slug => $taxonomy_name) {
$terms = get_the_terms( $post->ID, $taxonomy_slug);
if (is_array($terms) && count($terms) > 0) { ?>
<li class='apt-tax-item'>
<span class='apt-term-name'><?php echo $taxonomy_name ?></span>: 
<ul class='apt-tax-term-list'>
<?php $idx = 0;
foreach ( $terms as $term ) {
$idx++; ?>
<li class='apt-term-item'>:
<a class='apt-term-link' href = "<?php echo get_term_link($term); ?>"> <?php echo $term->name;
if ($idx < count($terms)){
echo ', ';
} ?></a>
</li>
<?php } ?>
</ul>
</li>
<?php
}
}
?>
</ul>