[英]Print only the array values - PHP
I have the following code within a WP loop 我在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;
}
}
?>
What I am looking to do is print only the WP_Term object slug to use in the classes. 我想做的是仅打印WP_Term对象子段以在类中使用。
<div class="courses-search-result standout-box-shadow <?php print_r( $classes ); ?>">
Now, I have got it working to a point and printing a value in the classes - but it is also printing the array structure: 现在,我可以使它工作到一定程度并在类中打印一个值-但它也可以打印数组结构:
class="courses-search-result standout-box-shadow cilex-clearfix Array
(
[0] => apprenticeship
)
"
Am I close to getting the correct code with my PHP? 我是否即将用PHP获得正确的代码? My knowledge of the language is basic right now so any help would be much appreciated.
我现在对这种语言的了解是很基础的,因此我们将不胜感激。
print_r
is a function intended for debugging, not formatted output. print_r
是用于调试的函数,而不是格式化的输出。
The simplest way to achieve what you want is using implode
, which combines the values of an array into a string; 实现目标的最简单方法是使用
implode
,它将数组的值组合成一个字符串。 and then echo
to display the string. 然后
echo
以显示字符串。
<div class="courses-search-result standout-box-shadow <?php echo implode(' ', $classes); ?>">
我建议将implode()
与PHP短回显标记结合使用(即<?=
-在5.4或更早版本中可用,并且启用short_open_tag配置设置):
<div class="courses-search-result standout-box-shadow <?= implode(' ', $classes ); ?>">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.