0
我正在嘗試將所有自定義帖子列表顯示爲主題主菜單上的子菜單。我需要動態添加它們,只要添加該類型的新帖子,而不是通過自定義菜單。 基本上我想有這樣的:Wordpress將子帖子添加到子菜單
Menu
- custom post 1
- custom post 2
- ...etc
如何才能做到這一點?
我正在嘗試將所有自定義帖子列表顯示爲主題主菜單上的子菜單。我需要動態添加它們,只要添加該類型的新帖子,而不是通過自定義菜單。 基本上我想有這樣的:Wordpress將子帖子添加到子菜單
Menu
- custom post 1
- custom post 2
- ...etc
如何才能做到這一點?
試試這個:
我剛纔添加過濾器被稱爲 「wp_nav_menu_items
」 請添加以下內容:
變化在你的菜單名稱,如果條件
2.更改您的帖子類型「cp_args」
請菜單和子菜單類添加類
add_filter('wp_nav_menu_items','add_todaysdate_in_menu', 10, 2);
function add_todaysdate_in_menu($items, $args) {
if($args->menu == 'Menu_name') { //change your menu name
$items .= '<li><a class="quick_enq" href="#">CUSTOM POST</a>';
$items .= '<ul>';
$cp_args = array('post_type'=>'YOUR_POST_TYPE','posts_per_page'=>-1);
$get_cp_query = new WP_Query($cp_args);
while($get_cp_query->have_posts()):$get_cp_query->the_post();
$items .= '<li><a class="sub_class" href="'.get_permalink($post->ID).'">'.$post->post_title.'</a></li>';
endwhile;
$items .= '</ul></li>';
}
return $items;
}
可以通過像https://wordpress.org/plugins/custom-post-type-auto-menu/插件來完成 –