从编辑页面/帖子编辑菜单项标题

时间:2015-05-01 作者:need-help

保存帖子/页面后,将在中创建的导航中创建一个新的菜单项(标题相同)nav-menus.php.

如果需要更改菜单项标题,需要转到nav-menus.php 把它换成别的东西。

是否可以有一些文本字段,例如在“编辑文章/页面”中,我们可以在不需要编辑的情况下为该页面/文章编写所需的菜单项标题nav-menus.php 每次?

这确实可以为用户节省大量时间。

Here is the full and working code! Thanks to @Howdy_McGee

/**
 * Adds a box to the main column on the Page edit screens.
 */
function menu_item_title_changer_add_meta_box() {

    $screens = array( \'page\' );

    foreach ( $screens as $screen ) {

        add_meta_box(
            \'menu_item_title_changer\',
            __( \'Menu item title changer\', \'webic-admin\' ),
            \'menu_item_title_changer_callback\',
            $screen
        );
    }
}
add_action( \'add_meta_boxes\', \'menu_item_title_changer_add_meta_box\' );

/**
 * Prints the box content.
 * 
 * @param WP_Post $post The object for the current post/page.
 */
function menu_item_title_changer_callback( $post ) {

    // Add a nonce field so we can check for it later.
    wp_nonce_field( \'menu_item_title_changer_meta_box\', \'menu_item_title_changer_meta_box_nonce\' );

    /*
     * Use get_post_meta() to retrieve an existing value
     * from the database and use the value for the form.
     */
    $value = get_post_meta( $post->ID, \'_menu_item_title_changer\', true );

    echo \'<label for="menu_item_title_changer_field">\';
    _e( \'Description for this field\', \'myplugin_textdomain\' );
    echo \'</label> \';
    echo \'<input type="text" id="menu_item_title_changer_field" name="menu_item_title_changer_field" value="\' . esc_attr( $value ) . \'" size="25" />\';
}

/**
 * When the post is saved, saves our custom data.
 *
 * @param int $post_id The ID of the post being saved.
 */
function menu_item_title_changer_save_meta_box_data( $post_id, $post ) {

    /*
     * We need to verify this came from our screen and with proper authorization,
     * because the save_post action can be triggered at other times.
     */

    // Check if our nonce is set.
    if ( ! isset( $_POST[\'menu_item_title_changer_meta_box_nonce\'] ) ) {
        return;
    }

    // Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $_POST[\'menu_item_title_changer_meta_box_nonce\'], \'menu_item_title_changer_meta_box\' ) ) {
        return;
    }

    // If this is an autosave, our form has not been submitted, so we don\'t want to do anything.
    if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) {
        return;
    }

    // Check the user\'s permissions.
    if ( isset( $_POST[\'post_type\'] ) && \'page\' == $_POST[\'post_type\'] ) {

        if ( ! current_user_can( \'edit_page\', $post_id ) ) {
            return;
        }

    } else {

        if ( ! current_user_can( \'edit_post\', $post_id ) ) {
            return;
        }
    }

    /* OK, it\'s safe for us to save the data now. */

    // Make sure that it is set.
    if ( ! isset( $_POST[\'menu_item_title_changer_field\'] ) ) {
        return;
    }


    // here is the code here it is working bug my system stack after i click save "save"
    if( \'page\' == $post->post_type ) {

        // Check Nonces

        $nav_item = new WP_Query( array(
            \'post_type\'      => \'nav_menu_item\',        // Nav Post Type
            \'posts_per_page\' => 1,                      // We only expect 1 result, if any
            \'meta_key\'       => \'_menu_item_object_id\', // With our Meta Key
            \'meta_value\'     => $post->ID               // And our Correct Page
        ) );

        if( $nav_item->have_posts() ) {                 // Ensure something was found
            wp_update_post( array(                      // Update the `nav_menu_item` Post Title
                \'ID\'            => $nav_item->posts[0]->ID,
                \'post_title\'    => htmlspecialchars( sanitize_text_field( $_POST[\'menu_item_title_changer_field\'] ) )
            ) );
        }

        // Update Page Meta
    }

    // Sanitize user input.
    $my_data = sanitize_text_field( $_POST[\'menu_item_title_changer_field\'] );

    // Update the meta field in the database.
    update_post_meta( $post_id, \'_menu_item_title_changer\', $my_data );

}
add_action( \'save_post\', \'menu_item_title_changer_save_meta_box_data\', 10, 3 );

1 个回复
最合适的回答,由SO网友:Howdy_McGee 整理而成

每当您向WordPress菜单添加新项目时(Appearance -> Menu ) 它将创建一个新的Post类型的Postnav_menu_item 并分配一个名为_menu_item_object_id 具有主Post ID。

例如,如果我创建一个名为“主页”的页面,它会被分配一个帖子ID2. 现在,当我转到菜单并将页面添加到菜单时,WordPress会创建一个新的post类型的postnav_menu_item, 将标题指定给“Home”,并在的数据库中为其指定ID3 并分配post meta_menu_item_object_id = 2. 如果我们想在菜单中将标题更改为“主页”,它会更改帖子ID3 标题为“主页”。

如果我们想通过metabox更改标题,我们需要找到帖子类型nav_menu_item 有post\\u meta_menu_item_object_id 使用与当前页面相同的ID。我假设您已经有了metabox设置,正在检查帖子类型和nonce。

if( \'page\' == $post->post_type ) {

    // Check Nonces

    $nav_item = new WP_Query( array(
        \'post_type\'      => \'nav_menu_item\',        // Nav Post Type
        \'posts_per_page\' => 1,                      // We only expect 1 result, if any
        \'meta_key\'       => \'_menu_item_object_id\', // With our Meta Key
        \'meta_value\'     => $post->ID               // And our Correct Page
    ) );

    if( $nav_item->have_posts() ) {                 // Ensure something was found
        wp_update_post( array(                      // Update the `nav_menu_item` Post Title
            \'ID\'            => $nav_item->posts[0]->ID,
            \'post_title\'    => htmlspecialchars( sanitize_text_field( $_POST[\'_textbox_name\'] ) )
        ) );
    }

    // Update Page Meta
}
在上述代码的末尾,您还需要将页面post meta更新为新标题,以便可以在metabox文本框中重新显示它,从而便于用户将来进行更新。

或者,如果您想在用户向菜单添加新项目时更新metabox文本框,您可以反转此过程:

if( \'nav_menu_item\' == $post->post_type ) {
    // Check Nonces

    $added_id   = get_post_meta( $post->ID, \'_menu_item_object_id\', true ); // This may or may not be a Page ID
    $type       = get_post_field( \'post_type\', $added_id );

    if( \'page\' == $type ) {
        // Update Page Metabox Textbox Here with `$post->post_title`
    }
}

结束

相关推荐

Show posts on front page only

我有一个旋转木马,我想被张贴在头版上,但由于某种原因,代码不起作用。条件语句<?php if ( is_home() ) : ?> <div class=\"hero\"> <div class=\"hero-carousel\"> <?php $catquery = new WP_Query( \'category_name=fea