1
votes

First time I have used stack-overflow, please forgive me if I make a faux pas.

I am new to php, and am currently building my first WordPress Theme. I want to have several separate sections of the website with a side-bar menu that displays links only to the other pages in the section you are in. For example if you are viewing post 1 of section A the side menu would display links to posts 2, 3, and 4 of section A. The menu would be the same on all posts of section A. Section B would have a different menu displaying it's posts. The site will require about 25-30 different sections.

I was hoping to do this with Categories, but am certainly open to other options. I have attempted this a few different ways to no success. I feel this should be simple, and I am just missing something basic.

I think my problem is calling the information necessary I started it as:

<?php if (is_category('tanzania')) : ?>

   <?php wp_nav_menu(array(
    'theme_location' => 'tanzania-menu'
   )) ?>

<?php elseif (is_category('safrica')) : ?>

   <?php wp_nav_menu(array(
    'theme_location' => 'safrica-menu'
   )) 

<?php else : print('Nope.'); ?>

<?php endif; ?>

This did not work at all (something I am sure is super obvious to you all). It displays each menu properly but only at the category overview page. Once you click on a link in the menu it takes you to the post's perma-url and the menu vanishes. I figured out that this is because the in_category only sees pages in the /category/ directory, but am unsure how to fix it.

The other issue is having my sidebar.php be so long. Using the above method I basically had a huge switch ~80 lines long. I am not sure if that is bad, but it seems... unwise. Everytime I tried to shrink it down, I ended up with it not working at all.

Thank you for your help with this, WP has been quite the learning curve.

1
Welcome to SO! Can you show some of the PHP code that you are currently working with? Also, with that, it would help to identify why it doesn't work for what you want. That way people can suggest ways to change or improve your code rather than having to create from scratch themselves.Matthew Green
Thank you Matthew! I will start editing right away!user2859308
Welcome to SO! Yes, this is completely possible - and can be done dynamically with much less than the 80 lines you have now :) If you could answer a few questions we'll be able to help you. Can you confirm that what you want is a menu in the sidebar of each post but the menu should only show links to other posts in the same category (but not the post you are currently viewing)? Doing this needs a little update to the loop for your single.php and another loop in the sidebar.php that only loops through current category (but does not include the currently viewed post in the list).William Patton
Thank you William! I want a menu that shows all the links in a given category, including the one you are on. So you are on Post 2 in Category A, then you can see links to Posts 1, 2, 3, and 4 all within Category A. Please let me know if I can be any more clear or if I can answer any other questions! And thank you once again!user2859308

1 Answers

0
votes

It's not working the way you want because once you leave the category page the rules no longer apply.

You could create a whole bunch of templates - one for each group of pages. In the template you define a custom sidebar into which you add the custom menus. But this is really clunky and time consuming.

Do you really, really need to make things so complicated for your visitors? They will get very lost and bewildered trying to find their way around the site if you keep changing the navigation form page to page.