帖子添加/编辑中的复选框可在菜单中添加/删除帖子

时间:2013-02-20 作者:Jonathan de M.

Introduction:

我已经为我的主题创建了几个帖子类型。要显示帖子类型中的帖子,我需要进入外观/菜单,然后手动添加帖子。

My Goal:

我想在帖子添加/编辑页面中添加两个单选按钮,以选择当前帖子是否在菜单中。

Attempts:

因此,我在post type添加/编辑页面中创建了复选框。看起来像enter image description here

一切正常,状态已保存,现在我想在保存之前将其作为子菜单添加/删除到菜单列表中,并将其作为父菜单的帖子类型,如何实现这一点?

以下是我迄今为止所做的工作:

// create post type
add_action( \'init\', \'create_post_type\' );
function create_post_type() {

    $conf = array(
        \'public\' => true,
        \'has_archive\' => true,
        \'menu_position\' => 5,
        \'show_in_nav_menus\' => true,
        \'hierarchical\' => true,
        \'has_archive\' => true,
        \'supports\' => array(\'title\', \'editor\', \'content\', \'page-attributes\'),
        \'taxonomies\' => array(\'category\', \'post_tag\'),

        );

    $conf[\'labels\'] = array(
        \'name\' => __( \'Architecte Posts\' ),
        \'singular_name\' => __( \'Architecte\' )
        );
    $conf[\'rewrite\'] = array(\'slug\' => \'architect\');

    register_post_type( \'architect_subpage\',$conf);


    $conf[\'labels\'] = array(
        \'name\' => __( \'Interior Posts\' ),
        \'singular_name\' => __( \'Interior\' )
        );
    $conf[\'rewrite\'] = array(\'slug\' => \'interior\');
    register_post_type( \'interior_subpage\',$conf);
}

// remove posts menu since it\'s not in use
function remove_menus () {
    global $menu;
    $restricted = array(__(\'Posts\'));
    end ($menu);
    while (prev($menu)){
        $value = explode(\' \',$menu[key($menu)][0]);
        if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
    }
}
add_action(\'admin_menu\', \'remove_menus\');




/* Define the custom box */
add_action( \'add_meta_boxes\', \'wpse_61041_add_custom_box\' );

/* Do something with the data entered */
add_action( \'save_post\', \'wpse_61041_save_postdata\' );

/* Adds a box to the main column on the Post and Page edit screens */
function wpse_61041_add_custom_box() {
    add_meta_box(
        \'wpse_61041_sectionid\',
        \'Publish in Menu\',
        \'wpse_61041_inner_custom_box\',
        \'architect_subpage\',
        \'side\',
        \'high\'
    );
}

/* Prints the box content */
function wpse_61041_inner_custom_box($post)
{
    // Use nonce for verification
    wp_nonce_field( \'wpse_61041_wpse_61041_field_nonce\', \'wpse_61041_noncename\' );

    // Get saved value, if none exists, "default" is selected
    $saved = get_post_meta( $post->ID, \'in_menu\', true);
    if( !$saved )
        $saved = \'no\';

    $fields = array(
        \'yes\'       => __(\'Yes\', \'wpse\'),
        \'no\'     => __(\'No\', \'wpse\'),
    );

    foreach($fields as $key => $label)
    {
        printf(
            \'<input type="radio" name="in_menu" value="%1$s" id="in_menu[%1$s]" %3$s />\'.
            \'<label for="in_menu[%1$s]"> %2$s \' .
            \'</label><br>\',
            esc_attr($key),
            esc_html($label),
            checked($saved, $key, false)
        );
    }
}

/* When the post is saved, saves our custom data */
function wpse_61041_save_postdata( $post_id )
{
      // verify if this is an auto save routine.
      // If it is our form has not been submitted, so we dont want to do anything
      if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
          return;

      // verify this came from the our screen and with proper authorization,
      // because save_post can be triggered at other times
      if ( !wp_verify_nonce( $_POST[\'wpse_61041_noncename\'], \'wpse_61041_wpse_61041_field_nonce\' ) )
          return;

      if ( isset($_POST[\'in_menu\']) && $_POST[\'in_menu\'] != "" ){

            update_post_meta( $post_id, \'in_menu\', $_POST[\'in_menu\'] );

      }
}
<支持>https://gist.github.com/roine/4994447

2 个回复
SO网友:brasofilo

以下只是概念证明,需要根据问题要求进行调整/改进。

它创建了一个带有下拉列表的元框,其中列出了所有可用的导航菜单。保存后,会将其添加到所选菜单中。

此问题(&;A被用作起点:Programmatically add a Navigation menu and menu items. 相关功能包括wp_get_nav_menuswp_update_nav_menu_item. 我不知道如何删除菜单项,但可能wp_delete_post 必须使用。

add_action( \'add_meta_boxes\', \'add_custom_box_wpse_87594\' );
add_action( \'save_post\', \'save_postdata_wpse_87594\', 10, 2 );

function add_custom_box_wpse_87594() 
{
    add_meta_box( 
        \'section_id_wpse_87594\',
        __( \'Available Nav-Menus\' ),
        \'inner_custom_box_wpse_87594\',
        \'post\',
        \'side\'
    );
}

function inner_custom_box_wpse_87594() 
{
    $navmenus = wp_get_nav_menus( array( \'hide_empty\' => false, \'orderby\' => \'none\' ) );

    // DEBUG
    // echo \'<pre>\' . print_r( $navmenus, true ) . \'</pre>\';

    wp_nonce_field( plugin_basename( __FILE__ ), \'noncename_wpse_87594\' );

    echo \'<select name="nav_menus_dropdown" id="nav_menus_dropdown">
        <option value="">- Select -</option>\';

    foreach( $navmenus as $m ) 
    {
        printf( 
            \'<option value="%s">%s</option>\',
            $m->term_id,
            $m->name
        );
    }

    echo \'</select>\';   
}

function save_postdata_wpse_87594( $post_id, $post_object ) 
{
    // Auto save?
    if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )  
        return;

    // Security
    if ( 
        !isset( $_POST[\'noncename_wpse_87594\'] ) 
        || !wp_verify_nonce( $_POST[\'noncename_wpse_87594\'], plugin_basename( __FILE__ ) ) 
        )
        return;

    // Correct post_type
    if ( \'post\' != $post_object->post_type )
        return;

    if( !empty( $_POST[\'nav_menus_dropdown\'] ) )
    {
        wp_update_nav_menu_item(
            $_POST[\'nav_menus_dropdown\'], 
            0, 
            array(
                \'menu-item-title\' => $_POST[\'post_title\'],
                \'menu-item-object\' => \'page\',
                \'menu-item-object-id\' => $_POST[\'ID\'],
                \'menu-item-type\' => \'post_type\',
                \'menu-item-status\' => \'publish\'
            )
        );
    }       
}
Result:
navigation menus meta box

SO网友:Jonathan de M.

我已经更改了brasofilo的代码,并在小时候添加了添加帖子的功能

下面是树脂:https://gist.github.com/roine/5105421/revisions

下面是完整的代码(也可提供here):

<?php
add_action( \'add_meta_boxes\', \'add_custom_box_wpse_87594\' );
add_action( \'save_post\', \'save_postdata_wpse_87594\', 10, 2 );

function add_custom_box_wpse_87594() 
{
    add_meta_box( 
        \'section_id_wpse_87594\',
        __( \'Available Nav-Menus\' ),
        \'inner_custom_box_wpse_87594\',
        \'post\',
        \'side\'
    );
}

function inner_custom_box_wpse_87594() 
{
    $navmenus = wp_get_nav_menus( array( \'hide_empty\' => false, \'orderby\' => \'none\' ) );

    // DEBUG
    // echo \'<pre>\' . print_r( $navmenus, true ) . \'</pre>\';

    wp_nonce_field( plugin_basename( __FILE__ ), \'noncename_wpse_87594\' );

    echo \'<select name="nav_menus_dropdown" id="nav_menus_dropdown">
        <option value="">- Select -</option>\';

    foreach( $navmenus as $m ) 
    {
        echo "<optgroup label=\'$m->name\'>";
        echo "<option value={$m->term_id}>&#8593; Add on top</option>";
        $items = wp_get_nav_menu_items( $m->term_id);
        foreach ($items as $key => $menu_item ) {
           if(!$menu_item->menu_item_parent){
              printf(\'<option value="%s-%s">%s</option>\',
              $m->term_id,
              $menu_item->object_id,
              $menu_item->title);
           }
           echo "</optgroup>";
        }

    echo \'</select>\';   
    }
}

function save_postdata_wpse_87594( $post_id, $post_object ) 
{
    // Auto save?
    if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )  
        return;

    // Security
    if ( 
        !isset( $_POST[\'noncename_wpse_87594\'] ) 
        || !wp_verify_nonce( $_POST[\'noncename_wpse_87594\'], plugin_basename( __FILE__ ) ) 
        )
        return;

    // Correct post_type
    if ( \'post\' != $post_object->post_type )
        return;

    if( !empty( $_POST[\'nav_menus_dropdown\'] ) )
    {
        $parents = explode(\'-\', $_POST[\'nav_menus_dropdown\']);
        $item = array(
            \'menu-item-title\' => $_POST[\'post_title\'],
            \'menu-item-object\' => \'page\',
            \'menu-item-object-id\' => $_POST[\'ID\'],
            \'menu-item-type\' => \'post_type\',
            \'menu-item-status\' => \'publish\'
        );
        if(count($parents) > 1){
           $item[\'menu-item-parent-id\'] = $parents[1];
        }
        wp_update_nav_menu_item(
            $_POST[\'nav_menus_dropdown\'], 
            0, 
            $item
        );
    }       
}
这对我来说很有效,但仍有待改进。

Behavior:

它还将在下拉列表中显示每个菜单的顶级项目Add on top 将作为顶层而不是子级添加帖子

enter image description here

结束

相关推荐

Order Admin sub-menu items?

我正在使用向CPT管理菜单添加项目add_submenu_page 这很好,但它们被添加到CPT选项后的子菜单底部。我希望能够将它们放在最上面,但我想这个问题也可以应用于订购所有基于管理员的子菜单项。我所尝试的(不起作用,我尝试了几种变体),function custom_menu_order($menu_ord) { if (!$menu_ord) return true; return array( \'edi