Disclaimer: dopo averlo provato, questo mi sembra un problema non più esistente, perché - almeno per me - funziona solo sulla mia installazione di WP 3.9.2. Tuttavia, non è stato possibile trovare un tracker di bug appropriato.
Ho messo insieme un piccolo plugin per testare questo, che potrebbe aiutare qualcuno. Ma come ho detto in precedenza, non ho potuto riprodurre il problema in un'installazione wordpress corrente. Ho separato il plug-in in quattro file, stanno andando insieme in una directory all'interno della directory del plug-in.
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
;
}
- Questo è un esempio di codice in qualche modo generalizzato.
- Deve essere adattato al caso d'uso reale.