0

我以编程方式在我的 WordPress 项目中添加了一些页面。为此,我正在使用wp_insert_post()函数。但是,这很好用。这些页面将自动添加到菜单中。对于其中一些很好,但我不想在那里添加的页面之一。

有没有办法在不手动删除的情况下防止这种情况发生。我正在考虑在创建页面后将其从菜单中删除,但我真的不知道如何,也许有更好/更简单的方法来做到这一点?

我创建页面的代码是:

    $inschrijfbevestiging = array(
        'post_title'   => 'Inschrijfbevestiging',
        'post_content' => '[inschrijfbevestiging]',
        'post_status'  => 'publish',
        'post_type'    => 'page'
    );
    wp_insert_post($inschrijfbevestiging);
4

3 回答 3

0

当您的菜单之前没有为一个theme_location设置时,会出现此问题

您必须为此theme_location设置一个菜单,这样它就不会自动更改。

于 2018-09-26T06:55:46.567 回答
0

当这种情况自动发生时,您可以在创建页面后将其从菜单中删除:

$objectWithPage = get_page_by_path('pageslug');
$menu_item_ids = wp_get_associated_nav_menu_items( $objectWithPage->ID, 'post_type' );

foreach ( (array) $menu_item_ids as $menu_item_id ) {
    wp_delete_post( $menu_item_id, true );
}
于 2018-09-28T12:07:13.373 回答
0

1.登录WordPress仪表板。

2.从仪表板左侧的“外观”菜单中,选择“菜单”选项以调出菜单编辑器。

3.选择页面顶部的创建新菜单

4.在菜单名称框中输入新菜单的名称

5.单击创建菜单按钮。

链接:https ://codex.wordpress.org/WordPress_Menu_User_Guide

于 2018-09-26T07:03:25.423 回答