最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Hide a menu item if user has posted a specific post_type - Stack Overflow

programmeradmin3浏览0评论

I'm trying to create a code snippet that will allow me to hide a menu item if a user has already posted at least 1 of a specific post type. I have this:

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);

But the above isn't working.

I would greatly appreciate it if someone could help me figure out where I'm going wrong.

发布评论

评论列表(0)

  1. 暂无评论