开发者

Expand Primary Links

开发者 https://www.devze.com 2023-01-10 05:36 出处:网络
How do you get children of primary links to output in the HTML? Only my parent-level primary links are generating LI\'s.

How do you get children of primary links to output in the HTML? Only my parent-level primary links are generating LI's.

I have a very very simple two-level primary links menu. There are 5 pages on the site: three parents, and the third parent has two children. In the administrative menu settings I have the parents' and childen's 'expanded' checkbox checked.

I have tried adding all kinds of functions from stackoverflow (including Drupal 6: Printing Unadulte开发者_如何学JAVArated Primary Links and all children) and drupal and elsewhere to template.php to get the children to output... nothing!

How do you make primary links expanded?? This seems like such a stupid question. Why wouldn't checking 'expand' checkboxes print the children?


A direct solution to your problem. For Drupal 6:

<?php 
  // gets whatever is set as the primary menu source, 
  // prints the whole tree, 
  // pays attention to expanded settings
  $menu_name = variable_get('menu_primary_links_source', 'primary-links');
  print menu_tree($menu_name);
?>

I then wrap that in a "primary" div id, in my page.tpl.php and page-front.tpl.php

Remember to set each menu item that has children to expanded if you want them to start laid out rather than collapsed. /admin/build/menu

This solution will take notice of your settings on this page: /admin/build/menu/settings

For more info see the Drupal 6 code on this page http://drupal.org/node/68578

The problem is displaying all the children of the primary menu, so the secondary menu is irrelevant, although it can be set to point to the 2nd level of the primary menu as the above link discusses.


I wasn't able to get primary links to print their children when using theme('links', $primary_links) in page.tpl.php regardless of configuration settings.

Instead, after hours of searching, I found this page on drupal.org which mentions that primary links will output differently depending on whether they're being used as links in a theme or as a block:

http://drupal.org/node/187932

It's a discussion about how primary links' 'expanded' option may appear as 'not working' when they are being used as a theme option instead of as a block. When used as a block, primary links will show the full hierarchy of the list you set up, including children. When used in the theme, unless you overwrite the default settings for how primary links display for your theme, it will only list the top-level primary links. This is what Mark mentioned as the default behavior above, and what I was experiencing.

To solve my issue, I simply took the code to print the menus out of the page template entirely, and then assigned primary links to the region in my template where the code was before.

so instead of:

<div id="nav">          
     <?php if ($primary_links): ?>
        <div id="primary">
          <?php print theme('links', $primary_links); ?>
        </div> 
      <?php endif; ?>

      <?php if ($secondary_links): ?>
        <div id="secondary">
          <?php print theme('links', $secondary_links); ?>
        </div> 
      <?php endif; ?>
</div>

I now have:

  <div id="nav">
  <?php if ($navigation): ?>
    <?php print $navigation ?>
  <?php endif ?>
 </div>

AND specified a region in my .info file for 'navigation', AND went to the block settings and assigned 'primary links' to my new navigation region.

To get rid of extraneous code outputting into my new navigation region, I also created a new block template for this region that just outputs the content of the block without any additional wrapping divs or heading tags. I ended up with a navigation unordered list that is pretty tidy, and I hope anyone else who is new to drupal and struggles with this issue finds my personal work-around helpful.


How primary links display is dependent on your theme. Without knowing more about your theme and how it implements primary links, it'd be impossible to say for sure why your primary links aren't expanding.

One thing you could check is the menu settings; in many themes, the second level is a separate menu called Secondary links. You can change this functionality by going to http://example.com/admin/build/menu/settings and setting Source for the secondary links to Primary links: this will display the second level of the primary links menu instead of the (likely empty) secondary links menu.


This brilliant blog post tells you exactly how you can get the entire tree structure for your primary links (including children) programmatically, and even shows you a way of tidying up this structure for easier use.

http://jamesmorrish.co.uk/blog/get-a-clean-array-of-primary-links-in-drupal/


I believe the Menu block module does what you are looking for. Some more details about this module (from its project page):

So… have you ever used the Main and Secondary menu links feature on your theme and wondered “how the hell do I display any menu items deeper than that?”

Well, that’s what this module does. It provides configurable blocks of menu trees starting with any level of any menu. And more!

So if you’re only using your theme’s Main menu links feature, you can add and configure a “Main menu (levels 2+)” block. That block would appear once you were on one of the Main menu’s pages and would show the menu tree for the 2nd level (and deeper) of your Main menu and would expand as you traversed down the tree. You can also limit the depth of the menu’s tree (e.g. “Main menu (levels 2-3)”) and/or expand all the child sub-menus (e.g. “Main menu (expanded levels 2+)”).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号