目前我们正在使用wp_list_pages
显示子页面列表。然而,我们希望获得每个页面的类别,并根据每个页面所属的类别在前端显示一些CSS。这是我们当前使用的查询:
wp_list_pages( \'post_status=publish&post_type=board-meetings&title_li=\' );
有没有办法获取每页的类别?目前我们正在使用wp_list_pages
显示子页面列表。然而,我们希望获得每个页面的类别,并根据每个页面所属的类别在前端显示一些CSS。这是我们当前使用的查询:
wp_list_pages( \'post_status=publish&post_type=board-meetings&title_li=\' );
有没有办法获取每页的类别?您可以使用get_pages()
然后,对于每个页面,使用wp_get_object_terms
. 您的代码可能如下所示:
$args = array(
\'post_type\' => \'board-meeting\',
\'post_status\' => \'publish\'
);
$pages = get_pages($args);
if ( ! empty( $pages ) ) {
echo \'<ul>\';
foreach($pages as $page){
$cats = wp_get_object_terms( $page->ID, \'category\' ); // Array of categories
$cat = \'\';
if ( ! empty( $cats ) ) {
if ( ! is_wp_error( $cats ) ) {
$cat = $cats[0]->slug; // slug of 1st category in the array $cats
}
// slug of category used as CSS class name for anchor tag
echo \'<li><a class=\'. $cat.\' href="\' . get_permalink($page-ID) . \'">\' . esc_html( $page->post_title ) . \'</a></li>\';
}
}
echo \'</ul>\';
}
您可以根据需要调整代码。我希望这有帮助。
当我使用下面的代码时<?php wp_nav_menu( array(\'menu\' => \'categories\' )); ?> 我可以创建一个新的菜单来列出我创建的wordpress中的所有类别。我用它在页面中间列出所有类别。我现在的问题是:有没有一种简单的方法可以为存在的每个子类别创建下拉菜单?那么,当我点击一个特定的类别时,它的子类别会显示出来吗?