我想创建一个类别列表,其中当前类别具有不同的布局。但是,我无法让foreach工作...
当$ category等于$ current_category时,我试图使foreach跳过(继续)。但是,它不会跳过而是输出所有值
<?php
$args = array(
'orderby' => 'slug',
'parent' => 0
);
$categories = get_categories( $args );
$current_category = single_cat_title("", false);
foreach ( $categories as $category ) {
if ($current_category == $category) {
continue;
}
else{
echo '<li class="categorieknop"><a href="' . get_category_link( $category->term_id ) . '" rel="bookmark">' . $category->name . '' . '' . $category->description . '</a></li>';
}
}
?>
Expected不是当前类别的输出,但是,它确实会输出当前类别...
答案 0 :(得分:0)
感谢@stender,他指出我正在将对象与字符串进行比较,从而得出正确的代码:
<?php
$args = array(
'orderby' => 'slug',
'parent' => 0
);
$categories = get_categories( $args );
$current_category = single_cat_title("", false);
foreach ( $categories as $category ) {
if ($current_category === $category->name) {
echo '<li class="current categorieknop"><a href="' . get_category_link( $category->term_id ) . '" rel="bookmark">' . $category->name . '' . '' . $category->description . '</a></li>';
continue;
}
else{
echo '<li class="categorieknop"><a href="' . get_category_link( $category->term_id ) . '" rel="bookmark">' . $category->name . '' . '' . $category->description . '</a></li>';
}
}
?>