至少应该从这些方面着手。您需要先注册菜单并获取ID。我没有删除类别,但您也需要注意这一点。
This is largely untested. Back up your DB first!!
add_action( \'created_category\', \'my_update_category_menu\' );
add_action( \'delete_category \', \'my_delete_category_menu\' );
function my_update_category_menu ($cat) {
$category = get_category($cat);
$menu_id = yourMenuIdHere; // <--------- YOUR ID
wp_update_nav_menu_item($menu_id, 0, array(
\'menu-item-title\' => __($category->name),
\'menu-item-classes\' => $category->name,
\'menu-item-url\' => home_url( get_category_link( $category->term_id ) ),
\'menu-item-status\' => \'publish\')
);
}
function my_delete_category_menu ($cat) {
$category = get_category($cat);
// not sure how to get the backwards look up but menu items are stored as posts
// http://codex.wordpress.org/Function_Reference/wp_delete_post
}
如果您想批量添加现有类别(尽管通过管理员手动添加很容易),应该可以按照以下方式进行操作。运行一次并将其从函数中删除。php。
$categories = get_categories();
foreach($categories as $category) {
wp_update_nav_menu_item($menu_id, 0, array(
\'menu-item-title\' => __($category->name),
\'menu-item-classes\' => $category->name,
\'menu-item-url\' => home_url( get_category_link( $category->term_id ) ),
\'menu-item-status\' => \'publish\')
);
}