Search code examples
wordpresspluginsbuddypress

Buddypress Selected first subnav


I have a new nav and two subnavs. They are working properly but I want to have the first subnav selected when I click in the nav item, because I don´t want to have two differents content inhalt.

The name of the new nav is: 'Jobs' and the names of the two subnavs are: 'All Jobs' and 'Add new Job'. I want that wenn I click in 'Jobs' i have the list of all Jobs, then 'All Jobs' would be clicked. Buddypress make the same in for example the tab 'Profile' and the first subnav.

My code:

function profile_tab_jobs() {
    global $bp;

    bp_core_new_nav_item( array(
        'name'                => 'Jobs',
        'slug'                => 'jobs',
        'screen_function'     => 'jobs_screen',
        'position'            => 40,
        'parent_url'          => bp_loggedin_user_domain() . '/jobs/',
        'parent_slug'         => $bp->profile->slug,
        'default_subnav_slug' => 'jobs_subnav',
    ) );

    bp_core_new_subnav_item( array(
        'name'              => 'All Jobs',
        'slug'              => 'all_jobs',
        'parent_url'        => trailingslashit( bp_displayed_user_domain() . 'jobs' ),
        'parent_slug'       => 'jobs',
        'screen_function'   => 'all_job_screen',
        'position'          => 100,
        'user_has_access'   => bp_is_my_profile()
    ) );

    bp_core_new_subnav_item( array(
        'name'              => 'Add new Job',
        'slug'              => 'new_job',
        'parent_url'        => trailingslashit( bp_displayed_user_domain() . 'jobs' ),
        'parent_slug'       => 'jobs',
        'screen_function'   => 'add_job_screen',
        'position'          => 110,
        'user_has_access'   => bp_is_my_profile()
    ) );

}
add_action( 'bp_setup_nav', 'profile_tab_jobs' );

WordPress 4.8.2

Thank you 🙂


Solution

  • I find a solution. I change the value 'default_subnav_slug' in bp_core_new_nav_item (subnav slug).

    function profile_tab_jobs() {
        global $bp;
    
        bp_core_new_nav_item( array(
            'name'                 => 'Jobs',
            'slug'                 => 'jobs',
            'screen_function'      => 'jobs_screen',
            'position'             => 40,
            'parent_url'           => bp_loggedin_user_domain() . '/jobs/',
            'parent_slug'          => $bp->profile->slug,
            'default_subnav_slug'  => 'all_jobs',
        ) );
    
        bp_core_new_subnav_item( array(
            'name'              => 'All Jobs',
            'slug'              => 'all_jobs',
            'parent_url'        => trailingslashit( bp_displayed_user_domain() . 'jobs' ),
            'parent_slug'       => 'jobs',
            'screen_function'   => 'jobs_screen',
            'position'          => 100,
            'user_has_access'   => bp_is_my_profile()
        ) );
    
        bp_core_new_subnav_item( array(
            'name'              => 'Add new Job',
            'slug'              => 'new_job',
            'parent_url'        => trailingslashit( bp_displayed_user_domain() . 'jobs' ),
            'parent_slug'       => 'jobs',
            'screen_function'   => 'add_job_screen',
            'position'          => 110,
            'user_has_access'   => bp_is_my_profile()
        ) );
    
    }
    add_action( 'bp_setup_nav', 'profile_tab_jobs' );