我搜索过高和低,我有一个自定义帖子类型,我希望将这些单曲添加到菜单父项中。我期望在Google上找到大量的帖子,以向正确的方向推我,并提出了以下建议:
function artists_menu_filter( $items, $menu, $args ) {
$children = array();
$order = count($items);
$parent = 0;
foreach ( $items as $item ) {
if ( in_array('artists-parent-item', $item->classes) ) {
$parent = $item->ID;
}
}
if( $parent > 0 ) {
foreach ( get_posts( 'post_type=artists&numberposts=-1&orderby=title&order=ASC' ) as $post ) {
$post->menu_item_parent = $parent;
$post->menu_order = ++$order;
$post->object = 'custom';
$post->post_type = 'nav_menu_item';
$post->title = $post->post_title;
$post->type = 'custom';
$post->url = get_permalink( $post->ID );
array_push($children, $post);
}
}
return array_merge( $items, $children );
}
add_filter( 'wp_get_nav_menu_items', 'artists_menu_filter', 10, 3 );
这真的是正确的方法吗?可以用更少的代码来完成吗?它可以正常工作,所以我没有抱怨,但实际上是想让Wordpress引起我的注意