WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom Menu (5 posts)

  1. marmon
    Member
    Posted 1 year ago #

    Using other posts from this forum I have been able to learn how to display a different custom menu in the sidebar of each page and category. Here's an example of what I've done...

    <?php
    if (is_page('19')){
    wp_nav_menu(array('menu'=>'menu-name-1', ));
    } elseif (is_page('20')) {
    wp_nav_menu(array('menu'=>'menu-name-2' ));
    } elseif (is_page('21')) {
    wp_nav_menu(array('menu'=>'menu-name-3' ));
    }
    ?>

    This works fine, but what I would like is not to have to add a new elseif every time I add a page - I'd rather have a collection of page numbers for each menu.

    So far, my attempts to do this have failed. I have tried (is_page('19', 20', '21')) and (is_page('19' || '20' || '21')) and both of these without quote marks.

    I just want my code to look better and be easier to maintain. Any ideas what I could be missing or do I just have to suffer an ever-increasing line of elseifs?

    Thanks in advance!

    PS Sorry, can't direct you to my site as it's not public yet.

  2. Zoe Rooney
    Member
    Posted 1 year ago #

    The correct way to combine pages into a single statement is to use an array:

    is_page( array( 19, 20, 21 ) )

    See: http://codex.wordpress.org/Conditional_Tags#A_PAGE_Page

  3. marmon
    Member
    Posted 1 year ago #

    Yes, that works. Thank you so much. I knew I must be missing something obvious!

    Thank you again :-)

  4. Prometheus Fire
    Member
    Posted 1 year ago #

    You might consider using the Dynamic widgets plugin. It would save you working in the code and you can configure it based on a huge range of factors. If you prefer working in code, then perhaps examining the code of the plug in will give you an idea of the direction to go. There are other plug insurance that do similar things but I'm not too familiar with them because I've been so pleased with dynamic widgets that I'm keep going back to it whenever I need some functionality like you are describing. I find it fast and easy to use.

    Link: http://wordpress.org/extend/plugins/dynamic-widgets/

  5. marmon
    Member
    Posted 1 year ago #

    Thanks, but I actually prefer working with the code and using forums where I need to fill in the gaps. I find it more satisfying and feel that it's better not to rely on too many plugins. Perhaps one day I will change my mind though :-)

Topic Closed

This topic has been closed to new replies.

About this Topic