• Resolved marmon

    (@marmon)


    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.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The correct way to combine pages into a single statement is to use an array:

    is_page( array( 19, 20, 21 ) )

    See: https://codex.www.ads-software.com/Conditional_Tags#A_PAGE_Page

    Thread Starter marmon

    (@marmon)

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

    Thank you again ??

    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: https://www.ads-software.com/extend/plugins/dynamic-widgets/

    Thread Starter marmon

    (@marmon)

    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 ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom Menu’ is closed to new replies.