我有一个获取类别名称的 wordpress foreach 循环。
$i = 0;
foreach($catnamea as $legname ) {
$i++;
echo $legname[$i].'<br>';
}
如果我去 $legname[0] - 它会给我一个类别名称,当我增加数字时,它会列出所有其他名称。但是我不需要输入另一个数字就可以发生这种情况,我非常希望它在变量 $i 上为每个项目添加一个数字。
这是完整的代码,所以你可以看到发生了什么
var pieData = [
<?php
$user_id = get_query_var('author');
$rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
//get all posts from author
$args = array(
'post_type' => 'post',
'author'=> $queried_object->ID
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
//put categories in array
$cat = get_the_category( get_the_ID() );
$terms[] = $cat[0]->term_id;
$catnames[] = $cat[0]->cat_name;
endwhile;
wp_reset_query();
endif;
//count matching categories (array vals)
$countVal = array_count_values($terms);
$colors = array();
$catnames[] = array();
foreach($countVal as $count){
$color = '#'.$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)];
echo " {
value: ".$count.",
color:'".$color."'
},";
$colors[] = $color;
$catnamea[] = $catnames;
}
?>
]
var myPie = new Chart(document.getElementById("piec").getContext("2d")).Pie(pieData);
</script>
<?php
foreach($colors as $scolor) {
echo '<div style="background:'.$scolor.';height:5px; width:5px;float:left"></div>';
}
$i = 0;
foreach($catnamea as $legname ) {
$i++;
echo $legname[$i].'<br>';
} ?>