wordpress 类别菜单中的当前类别
Current cateogry in wordpress category menu
我有一个小脚本可以显示任何包含帖子的类别并显示它们(就像一个小菜单)
<?php
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC'
) );
echo '<a class="blog-panel-cat-menu bg-color-1" href="' . get_permalink( get_option( 'page_for_posts' ) ) . '">All</a>';
foreach( $categories as $category ) {
if ($category->count > 0){
$category_link = sprintf(
'<a class="blog-panel-cat-menu bg-color-1" href="%1$s" alt="%2$s">%3$s</a>',
esc_url( get_category_link( $category->term_id ) ),
esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ),
esc_html( $category->name )
);
echo $category_link;
}
}
?>
当点击任何 link 时,它将重定向到显示该类别帖子的页面,例如 mywebsite/news/category/blog/
或 mywebsite/news/category/news/
(mywebsite/news
是博客主页显示所有类别)
当我在 mywebsite/news/category/blog/
时,我希望菜单中的 "blog" link 在当前 [=24] 之后有 class blog-cat-focus
=] 在这样的 foreach 循环中 <a class="blog-panel-cat-menu bg-color-1 blog-cat-focus" href="%1$s" alt="%2$s">%3$s</a>
试试这个代码。添加了两行来检查当前类别。
<?php
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC'
) );
$category = get_category( get_query_var( 'cat' ) );
echo $cat_id = $category->cat_ID;
echo '<a class="blog-panel-cat-menu bg-color-1" href="' . get_permalink( get_option( 'page_for_posts' ) ) . '">All</a>';
foreach( $categories as $category ) {
if ($category->count > 0){
$cust_class = '';
if($category->term_id==$cat_id){$cust_class = 'blog-cat-focus';}
$category_link = sprintf(
'<a class="blog-panel-cat-menu bg-color-1 %4$s>" href="%1$s" alt="%2$s">%3$s</a>',
esc_url( get_category_link( $category->term_id ) ),
esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ),
esc_html( $category->name ),
$cust_class
);
echo $category_link;
}
}
?>
我有一个小脚本可以显示任何包含帖子的类别并显示它们(就像一个小菜单)
<?php
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC'
) );
echo '<a class="blog-panel-cat-menu bg-color-1" href="' . get_permalink( get_option( 'page_for_posts' ) ) . '">All</a>';
foreach( $categories as $category ) {
if ($category->count > 0){
$category_link = sprintf(
'<a class="blog-panel-cat-menu bg-color-1" href="%1$s" alt="%2$s">%3$s</a>',
esc_url( get_category_link( $category->term_id ) ),
esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ),
esc_html( $category->name )
);
echo $category_link;
}
}
?>
当点击任何 link 时,它将重定向到显示该类别帖子的页面,例如 mywebsite/news/category/blog/
或 mywebsite/news/category/news/
(mywebsite/news
是博客主页显示所有类别)
当我在 mywebsite/news/category/blog/
时,我希望菜单中的 "blog" link 在当前 [=24] 之后有 class blog-cat-focus
=] 在这样的 foreach 循环中 <a class="blog-panel-cat-menu bg-color-1 blog-cat-focus" href="%1$s" alt="%2$s">%3$s</a>
试试这个代码。添加了两行来检查当前类别。
<?php
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC'
) );
$category = get_category( get_query_var( 'cat' ) );
echo $cat_id = $category->cat_ID;
echo '<a class="blog-panel-cat-menu bg-color-1" href="' . get_permalink( get_option( 'page_for_posts' ) ) . '">All</a>';
foreach( $categories as $category ) {
if ($category->count > 0){
$cust_class = '';
if($category->term_id==$cat_id){$cust_class = 'blog-cat-focus';}
$category_link = sprintf(
'<a class="blog-panel-cat-menu bg-color-1 %4$s>" href="%1$s" alt="%2$s">%3$s</a>',
esc_url( get_category_link( $category->term_id ) ),
esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ),
esc_html( $category->name ),
$cust_class
);
echo $category_link;
}
}
?>