Ausblenden Sie ein Menüelement, wenn der Benutzer ein bestimmtes post_type gepostet hat
Posted: 08 Feb 2025, 04:07
Ich versuche, ein Code -Snippet zu erstellen, mit dem ich ein Menüelement ausblenden kann, wenn ein Benutzer bereits mindestens 1 eines bestimmten Post -Typs veröffentlicht hat. Ich habe das: < /p>
function hide_specific_menu_item($items, $args) {
$user_id = get_current_user_id(); //get the user_id
$post_type = "student"; // define the post type
$posts = count_user_posts( $user_id, $post_type ); //count user's posts
if($posts > 0){
foreach ($items as $key => $item) {
if ($item->title === 'Add Student') { //hide the menu item
unset($items[$key]);
}
}
}
return $items;
}
add_filter('wp_nav_menu_objects', 'hide_specific_menu_item', 10, 2);
< /code>
Aber das obige funktioniert nicht. /P>
function hide_specific_menu_item($items, $args) {
$user_id = get_current_user_id(); //get the user_id
$post_type = "student"; // define the post type
$posts = count_user_posts( $user_id, $post_type ); //count user's posts
if($posts > 0){
foreach ($items as $key => $item) {
if ($item->title === 'Add Student') { //hide the menu item
unset($items[$key]);
}
}
}
return $items;
}
add_filter('wp_nav_menu_objects', 'hide_specific_menu_item', 10, 2);
< /code>
Aber das obige funktioniert nicht. /P>