Bag*_*eye 1 php arrays wordpress
我在WP循环中有以下代码
<?php
// Get the selected taxonomies for the post type
$terms = get_the_terms( get_the_ID(), 'course_type' );
$classes = array();
if ( $terms && ! is_wp_error( $terms ) ){
foreach ( $terms as $term) {
$classes[] = $term->slug;
}
}
?>
Run Code Online (Sandbox Code Playgroud)
我要做的是只打印要在类中使用的WP_Term对象slug.
<div class="courses-search-result standout-box-shadow <?php print_r( $classes ); ?>">
Run Code Online (Sandbox Code Playgroud)
现在,我已经让它工作到一个点并在类中打印一个值 - 但它也打印了数组结构:
class="courses-search-result standout-box-shadow cilex-clearfix Array
(
[0] => apprenticeship
)
"
Run Code Online (Sandbox Code Playgroud)
我接近用PHP获取正确的代码吗?我对这门语言的了解是基本的,所以任何帮助都会非常感激.
print_r 是一个用于调试的函数,而不是格式化的输出.
实现您想要的最简单的方法是使用implode,它将数组的值组合成一个字符串; 然后echo显示字符串.
<div class="courses-search-result standout-box-shadow <?php echo implode(' ', $classes); ?>">
Run Code Online (Sandbox Code Playgroud)