0

I am using Parabola theme of Wordpress, and i would like to display few dynamic menu links for logged in users, just at the right of the header title. As shown in the below image, Menu 1 and Menu 2 links should be placed at the right of header title.

enter image description here

What would be the best approach for this? I don't prefer a plugin, but instead like to use theme hooks, because when the theme changes, i can just write a hook for achieving the same functionality. Please suggest where should i modify code, or what new functions i have to write.

EDIT:

I would like to keep the main menu as it is. I only want custom menu links after header title.

2 Answers 2

2

If your theme includes a header hook, you can simply hook in a new widget in that area and include the conditional tag for logged in users which only displays the custom menu widget with the links you want.

<?php
if ( is_user_logged_in() ) {
 wp_nav_menu( array( 'theme_location' => 'logged-in-menu' ) );
} else {
 wp_nav_menu( array( 'theme_location' => 'logged-out-menu' ) );
}
?>

You could use this code above directly in your header.php file or remove the PHP tags and include it in a custom function with your theme specific hooks (If has one) and use it in your child themes functions file.

Another option is to create a new widget area and use the widget visibility or another conditional widget which enables you to add the conditional directly to the widget.

If you can provide the theme specific header hook, i can provide the code to create a new widget area.

Source http://codex.wordpress.org/Function_Reference/wp_nav_menu

function wpsites_register_header_widget() {

register_sidebar( array(
'name' => 'Header Widget',
'id' => 'header-widget',
'before_widget' => '<div>',
'after_widget' => '</div>',
) );
}

add_action( 'widgets_init', 'wpsites_register_header_widget' );

add_filter( 'your_themes_header_hook', 'wpsites_header_widget' );

function wpsites_header_widget() {


if ( is_user_logged_in() && is_active_sidebar( 'header-widget' ) ) { 
dynamic_sidebar('header-widget', array(
'before' => '<div class="header-widget">',
'after' => '</div>',
) );


    }

}

Drag in a custom Menu Widget and you're set.

7
  • I have this code in my theme's header.php: cryout_header_hook(); wp_head(); Jan 3, 2014 at 10:33
  • I placed your code in functions.php, but no change occurred. Jan 3, 2014 at 10:47
  • Then the hook you provided isn't correct. You can change the hook to any WordPress hook like loop_start to see the code work. You must also populate the new widget area with any type of widget to test it. Jan 3, 2014 at 10:48
  • I have created a sample Text widget under Header Widget in my admin... is that what is required? By the way, dynamic_sidebar(....) was not working in above code. Jan 3, 2014 at 11:29
  • That could be because of your theme but the code is tested on the default themes and works. I suggest you consult your theme support forum. Jan 3, 2014 at 11:42
2

Generate your menu in admin and after_login replace your menu name:

<?php if (is_user_logged_in() ) {  ?>
          <?php wp_nav_menu( array('menu'=>'after_login','menu_class' => 'mainmenu' ) ); ?>
<?php }?>

if( current_user_can( 'administrator' ) ){} // only if administrator
if( current_user_can( 'editor' ) ){} // only if editor
if( current_user_can( 'author' ) ){} // only if author
if( current_user_can( 'contributor' ) ){} // only if contributor
if( current_user_can( 'subscriber' ) ){} // only if subscriber

Or:

if( current_user_can( 'level_10' ) ){}
if( current_user_can( 'level_9' ) ){}
if( current_user_can( 'level_8' ) ){}
if( current_user_can( 'level_7' ) ){}
if( current_user_can( 'level_6' ) ){}
if( current_user_can( 'level_5' ) ){}
if( current_user_can( 'level_4' ) ){}
if( current_user_can( 'level_3' ) ){}
if( current_user_can( 'level_2' ) ){}
if( current_user_can( 'level_1' ) ){}
if( current_user_can( 'level_0' ) ){}
5
  • But instead of creating the menu in the admin panel, i would like to achieve this with coding, so that i can even control which menu links must be shown to which type of user logged in (admin or normal user). Jan 3, 2014 at 10:08
  • this for all user type. if user ya admin or any user type login then display this menu. i have add for this one conditions.
    – Ravi Patel
    Jan 3, 2014 at 10:10
  • Please see my edited question. I do't want to replace the main menu. Jan 3, 2014 at 10:13
  • not replace main menu but create new one for two links wp-admin in Apperance->menus-> in create a new menu.
    – Ravi Patel
    Jan 3, 2014 at 10:17
  • After creating a new menu in admin panel, hope i need to substitute the value of "menu_class" in the above code, right? Jan 3, 2014 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.