免责声明:尝试一下之后,这对我来说似乎不再是一个存在的问题,因为-至少对我而言-它仅适用于WP 3.9.2安装。但是找不到合适的错误跟踪器。
我在一起有一个小插件来测试这一点,这可能会对某人有所帮助。但是正如我在上述免责声明中所说,我无法在当前的wordpress安装中重现该问题。我将插件分为四个文件,它们一起进入了插件目录中的一个目录。
plugin-cpt_menu_hierarchy.php:
<?php
defined( 'ABSPATH' ) OR exit;
/**
* Plugin Name: CPT Menu Hierarchy Fix?
* Description: CPT Menu Hierarchy Fix?
* Author: ialocin
* Author URL: http://wordpress.stackexchange.com/users/22534/ialocin
* Plugin URL: http://wordpress.stackexchange.com/q/13308/22534
*/
// registering nonsense post type
include 'include-register_post_type.php';
// adding meta box to nosense custom post type
include 'include-cpt_parent_meta_box.php';
// menu highlighting fix
include 'include-menu_highlighting.php';
include-register_post_type.php:
<?php
defined( 'ABSPATH' ) OR exit;
// See: http://codex.wordpress.org/Function_Reference/register_post_type
add_action( 'init', 'wpse13308_basic_reigister_post_type');
function wpse13308_basic_reigister_post_type() {
$args = array(
'public' => true,
'label' => 'Nonsense'
);
register_post_type( 'nonsense', $args );
}
include-cpt_parent_meta_box.php:
<?php
defined( 'ABSPATH' ) OR exit;
// pretty much like @bainternet's answer
// Add Meta Box
add_action( 'add_meta_boxes', 'nonsense_add_meta_box' );
function nonsense_add_meta_box() {
add_meta_box(
'nonsense',
__( 'Nonsense parent' ),
'nonsense_inner_meta_box',
'nonsense'
);
}
// Meta Box Content
function nonsense_inner_meta_box() {
global $post;
wp_nonce_field(
plugin_basename( __FILE__ ),
'nonsense_inner_meta_box'
);
echo 'Parent Page: ';
$mypages = get_pages();
echo '<select name="cpt_parent">';
foreach($mypages as $page){
echo '<option value="'.$page->ID.'"';
if ($page->ID == $post->post_parent) {echo ' selected';}
echo '>'.$page->post_title.'</option>';
}
echo '</select>';
}
// Save Data From Meta Box
add_action( 'wp_insert_post_data', 'nonsense_save_meta_box_data' );
function nonsense_save_meta_box_data( $data, $postarr ) {
global $post;
if (
! wp_verify_nonce(
$_POST['nonsense_inner_meta_box'],
plugin_basename( __FILE__ )
)
) {
return $data;
}
if (
defined('DOING_AUTOSAVE')
&& DOING_AUTOSAVE
) {
return $data;
}
if ( $post->post_type == 'nonsense' ) {
$data['post_parent'] = $_POST['cpt_parent'];
}
return $data;
}
include-menu_highlighting.php:
<?php
defined( 'ABSPATH' ) OR exit;
// altering WordPress' nav menu classes via »nav_menu_css_class« filter
add_filter( 'nav_menu_css_class', 'wpse13308_fix_nav_menu_highlighting', 10, 2 );
function wpse13308_fix_nav_menu_highlighting( $classes, $item ) {
// data of the current post
global $post;
// setting up some data from the current post
$current_post_post_type = $post->post_type;
$current_post_parent_id = $post->post_parent;
// id of the post the current menu item represents
$current_menu_item_id = $item->object_id;
// do this for a certain post type
if( $current_post_post_type == 'nonsense' ) {
// remove unwanted highlighting class via array_filter and callback
// http://php.net/manual/de/function.array-filter.php
$classes = array_filter(
$classes,
'wpse13308_remove_highlighting_classes'
);
// when the parents id equals the menu items id, we want to
// highlight the parent menu item, so we check for:
if( $current_post_parent_id == $current_menu_item_id ) {
// use the css class used for highlighting
$classes[] = 'replace-with-css-class';
}
}
return $classes;
}
// callback to remove highlighting classes
function wpse13308_remove_highlighting_classes( $class ) {
return
(
// use the class(es) you need, overview over nav menu item css classes:
// http://codex.wordpress.org/Function_Reference/wp_nav_menu#Menu_Item_CSS_Classes
$class == 'highlight-class'
// uncomment next line if you want to check for more then one class
// repeat the line if you want to check for a third, fourth and so on
// || $class == 'replace-with-css-class'
)
? false
: true
;
}