PHP为逗号分隔的术语添加引号(PHP/WordPress)
我试图将两个连接在一起的术语分隔成逗号分隔的带引号的值 使用此代码:PHP为逗号分隔的术语添加引号(PHP/WordPress),php,wordpress,Php,Wordpress,我试图将两个连接在一起的术语分隔成逗号分隔的带引号的值 使用此代码: <?php $terms = get_the_terms( $post->ID, 'project_category' ); if ( $terms && ! is_wp_error( $terms ) ) : $links = array(); foreach ( $terms as $term
<?php
$terms = get_the_terms( $post->ID, 'project_category' );
if ( $terms && ! is_wp_error( $terms ) ) :
$links = array();
foreach ( $terms as $term )
{
$links[] = $term->name;
}
$links = str_replace(' ', '-', $links);
$tax = join(", ", $links);
else :
$tax = '';
endif;
?>
用这个输出
<div class="project-item col-sm-3 col-md-3" data-groups='[<?php echo strtolower($tax); ?>]'></div>
先试试这个:
foreach ( $terms as $term )
{
$links[] = '"'.$term->name.'"';
}
也许不是最好的,但一个非常简单的解决方案是在输出变量之前调整$tax
变量
$tax = '"'.str_replace(', ','", "',$tax).'"';
只要在不同的“术语”中没有逗号和空格,它就可以正常工作。哈利路亚,它可以工作!非常感谢你!
foreach ( $terms as $term )
{
$links[] = '"'.$term->name.'"';
}
$tax = '"'.str_replace(', ','", "',$tax).'"';