WordPress.org

Ready to get started?Download WordPress

Forums

Twenty Twelve
[resolved] Add Function to check if Navigation Widgets active (2 posts)

  1. aurooba
    Member
    Posted 1 year ago #

    Hi,

    As a disclaimer, I don't know any PHP, and this is my first venture into it. Basically, I widget-ized the navigation area in Twenty Twelve, but what I want to do is check if there are any widgets active in that area, and if not, use the default navigation menu coding. I know I'm doing it terribly wrong and probably breaking PHP practices.. here's what I've got..it doesn't work (breaks my installation), but I'm hoping someone can help me. :

    function checknav() {
    	if ( ! is_active_sidebar( 'nav-widgets' ) ) {
    				echo '<?php wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'nav-menu' ) ); ?>';
        }
    }
    add_action( 'widgets_init', 'checknav' );

    Any help is appreciated. Thanks!

  2. aurooba
    Member
    Posted 1 year ago #

    Nevermind! Fixed it!

    Instead of making a function, where I had the widget-ze code in header.php, I made it an if statement, so if nav-widgets wasn't active, it would insert code for regular menu, and if it is active, then show the widgets! :)

    <?php if ( ! is_active_sidebar( 'nav-widgets' ) ) {
    
    				wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'nav-menu' ) );
    
    } else {
        				?> <div id="nav-widgets"> <?
        				dynamic_sidebar('nav-widgets');
        				?> </div> <?
    } ?>

    But I'd love if someone could show me how to convert this into a function and then call it in the appropriate place in header.php.

Topic Closed

This topic has been closed to new replies.

About this Theme

About this Topic