对是的,它可以,您需要挂到助行器使用的过滤器上,例如:
add_filter( \'walker_nav_menu_start_el\', \'menu_show_project_post\', 10, 4 );
function menu_show_project_post( $item_output = \'\', $item = \'\', $depth = \'\', $args = \'\' ) {
global $post;
if ( is_array( $item->classes ) ) {
$query = false;
foreach( $item->classes as $class ) {
if ( $class == \'project_posts\') {
$query = true;
break;
}
}
if ( $query ) {
$the_posts = get_posts( array(
\'post_type\' => \'project\',
\'numberposts\' => 5
) );
if ( ! empty( $the_posts ) ) {
$new_html[] = \'<ul class="sub-menu">\';
foreach( ( array ) $the_posts as $post ) {
setup_postdata( $post );
$new_html[] = \'<li class="menu-item">\';
$new_html[] = \'<a href="\' . get_permalink( ) . \'" class="depth-\' . ( $depth ? $depth + 1 : 2 ) . \'">\';
$new_html[] = the_title(\'\',\'\',false );
$new_html[] = \'</a>\';
$new_html[] = \'</li>\';
}
wp_reset_postdata();
$new_html[] = \'</ul>\';
$item_output .= implode( "\\n", $new_html );
}
}
}
return $item_output;
}
在函数中使用此代码。php中,任何包含“project\\u posts”类的菜单项都会给出一个子菜单,其中包含5个project类型的posts。
根据需要修改代码,以显示任意数量的帖子或帖子类型