Avis de non-responsabilité: après l'avoir essayé, cela ne semble plus être un problème, car - au moins pour moi - cela fonctionne uniquement sur mon installation WP 3.9.2. Impossible de trouver un traqueur de bogues correspondant.
J'ai sorti ensemble un petit plugin pour tester cela, ce qui pourrait aider quelqu'un. Mais comme je l'ai dit dans l'avertissement ci-dessus, je ne pouvais pas reproduire le problème dans une installation wordpress actuelle. J'ai séparé le plugin en quatre fichiers, ils vont ensemble dans un répertoire à l'intérieur du répertoire du plugin.
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
;
}
- Il s'agit d'un exemple de code quelque peu généralisé.
- Il doit être adapté au cas d'utilisation réel.