Drupal 7 - Menu to UL - drupal

all. This question probably has a devilishly simple answer but it has kept me occupied for several hours.
I have my main menu and it's corresponding block in a Drupal site I am building. Like all other Drupal menus it contains a bunch of links to various parts of the site. I can assign it's block to a region and the menu links come out all nice and formatted with a title thing and little bullet points. The problem though is that I am making a custom theme for this website and I need to be able to work with the links without all the cruft added, preferably in something simple like an ul.
Is there any function that takes a menu and produces an ul containing all the links?
Maybe there is some way you can reduce the menu's block to just an ul.
I have been experimenting with theme_menu_tree(...) and theme(...) to no avail.
Thank you!

I find you can do most changes through CSS such as setting <H2> titles to display: none and setting <LI> tags to float: left for a horizontal navbar.
But... if you want to build your own menu from the Drupal data, here is some code from a site I'm working on. It builds a two-level menu. I'm sure you could simplify this code even further if you need.
//----------- primary menu (horizontal with drop-downs) -------------------------
$params = array('max_depth' => 3);
$menu = menu_build_tree('main-menu', $params);
$variables['menu'] = $menu;
$html = '<ul>';
foreach($menu as $item_menu) { //for each main element
$isSecondLevel = isset($item_menu['below']) && !empty($item_menu['below']);
if ($isSecondLevel) {
$html.= '<li>';
} else {
$html.= '<li class="sg">';
}
$html.= '<a class="topLevel" href="'.url($item_menu['link']['link_path']).'">';
$html .= $item_menu['link']['link_title'];
$html .= '</a>';
//is there any sub elements to display
if ($isSecondLevel) {
$html.= '<ul>';
foreach($item_menu['below'] as $item_submenu) { //for each sub element
$isThirdLevel=isset($item_submenu['below']) && ! empty($item_submenu['below']) ? 'main-menu-third_level' : '';
$html.= '<li>';
$html.= '<a href="'.url($item_submenu['link']['link_path']).'">';
$html.= $item_submenu['link']['link_title'];
$html.= '</a>';
$html.= '</li>';
}
$html.= '</ul>';
}
$html.= '</li>';
}
$html.= '</ul>';
$variables['main_menu_html'] = $html;
This code was placed inside function pinkribbon_process_page(&$variables) in template.php. Menu is printed in the template by calling <?php echo $main_menu_html ?>
Simon.
P.S. Others, please feel free to edit this code for clarity/simplicity.

I advice you to use
menu_tree_output
like this:
print render(menu_tree_output(menu_build_tree('main-menu', $parameters)));

You could call menu_build_tree and look at it's output and build a ul from it. However, despite the default menu output having loads of "cruft" it is a ul and should be themeable with CSS.
If you really want to build the menu yourself, I would reverse engineer another module that does so like Nice Menus

Related

how to display wp tags in dropdwon with checkbox in admin

I have Install WordPress and i need all tags should be display in drop down like category
Also I want search filter over there
when i want to put any comma separate tag in search box all tag should be checked in drop down automatically
You can use this function to get all the tags in the WP.
$tags = get_tags(array('get'=>'all'));
example,
$tags = get_tags(array('get'=>'all'));
$output .= '<ul class="tag-cloud-list">';
if($tags) {
foreach ($tags as $tag):
$output .= '<li>'. $tag->name .'</li>';
endforeach;
} else {
_e('No tags created.', 'text-domain');
}
$output .= '</ul>';
return $output;
The same way you can use the checkbox in the place of UL and LI. Also, you can use the add_meta_box() to add the tags to the all post type.
For more details, Please visit.
https://developer.wordpress.org/reference/functions/add_meta_box/
https://developer.wordpress.org/reference/functions/get_tags/

How to introduce a menu active effect in wordpress child theme

I have a challenge and will appreciate any assistance please.
I'm working with the wordpress sydney theme. I want to introduce an active(or current - if you like) effect to change the color of the menu of the current page.I have created a child theme, and have the necessary files in that folder but can't figure out where to effect the modification.I tried creating a new header file in the child theme to structure this but my website got crashed and i had to undo that. Kindly assist..
Using wp_get_nav_menu_items() you can apply active menu item effect.
<?php
$menu = wp_get_nav_menu_items('menu_name');
$id = get_the_ID();
foreach($menu as $menuitem)
{
if($id == $menuitem->object_id)
{
echo '<li class = "nav-active">'; //apply active class here
echo ''.$menuitem->title.'';
echo '</li>';
}
else
{
echo '<li>';
echo ''.$menuitem->title.'';
echo '</li>';
}
}
?>

How can I access (echo) a pages navigation label in Wordpress?

This is a continuation in a way of this question:
Wordpress Navigation Label in Browser tab
but I am afraid I need more guidance then the previous poster.
I too want to use the navigation label in the WordPress menu, to display the parent navigation label (and not the parent title!) before the page's actual title.
The code that works for the parent title is this:
<h1 class="page-title"><strong><?php
// If child page , also include the parent title:
if($post->post_parent)
{$parent_title = get_the_title($post->post_parent);
echo '<span>'.$parent_title.' — </span><br />';
}
the_title(); ?></strong></h1>
I would like to swap out .$parent_title. for the parent-navigation-label. Is there a way to echo that label?
Disclaimer: I am not a programmer by choice - only when I have to, so please be gentle...
Edit: This is the navigation label I am talking about btw:
The label is stored in the post object as title:
$parent = get_post( $post->post_parent );
echo '<span>' . $parent->title . ' — </span><br />';
(Ok - If there is a way to include formatted code in the comments - please let me know!)
Building on #diggys answer I added this:
<h1 class="page-title"><strong><?php
// Display the parent nav label:
$parent = get_post( $post->post_parent );
echo '<span>' . $parent->title . ' — </span><br />';
the_title(); ?>
</strong></h1>
but the output is empty - apart from the the — character (-). I must be missing something obvious, right?

Drupal 6: Printing Unadulterated Primary Links and all children

How in the WORLD is possible? I swear, I've read the equivalent of 3 encyclopedias to no avail. I've tried solutions within regions, page.tpl.php and blocks. None of them give me what I need... and I know there are so many other people that need this too!
I've come to the conclusion that I want to print out the menu within my page.tpl.php ... so no block solutions, please.
I want to be able to loop through the primary menu links (AND children) and rewrite the output so that there's no default Drupal class tagging. The closest I've found is this example:
<?php if (is_array($primary_links)) : ?>
<ul id="sliding-navigation">
<?php foreach ($primary_links as $link): ?>
<li class="sliding-element"><?php
$href = $link['href'] == "<front>" ? base_path() : base_path() . drupal_get_path_alias($link['href']);
print "<a href='" . $href . "'>" . $link['title'] . "</a>";
?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
As you can see, links are being reprinted with a custom UL and LI class ... that's GREAT! However, no children are being printed. How would I extend this code so that all children are a part of the list? NOTE: I don't want the children to only appear on their parent page, they must be present all the time. Otherwise, the drop-down menu I have planned is useless.
I sincerely thank you in advance to lessening my gargantuan headache!
It's hard to affect the output once it's got as far as the page.tpl - you might do better looking for template.php functions.
This is one I used to alter the classes of my primary links:
function primary_links_add_icons() {
$links = menu_primary_links();
$level_tmp = explode('-', key($links));
$level = $level_tmp[0];
$output = "<ul class=\"links-$level\">\n";
if ($links) {
foreach ($links as $link) {
$link = l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment']);
$output .= '<li class="sublevel">' . $link .'</li>';
};
$output .= '</ul>';
}
return $output;
}
And then in page.tpl.php I just called it like this:
<?php if ($primary_links) :?>
<?php print '<div id="menu">'; ?>
<?php print primary_links_add_icons(); ?>
<?php print '</div>'; ?>
<?php endif;?>
I had to add a <span> to my links for styling, so I overrode theme_links() in includes/theme.inc
You can copy the function to your template.php, rename it to yourthemename_links(), and modify it as needed.
This function outputs the ul, li tags, the drupal_attributes, classes of 'first', 'last', 'active', etc, and affects the menus throughout the site.
You may also want to check out the functions in includes/menu.inc, including theme_menu_local_tasks() and menu_local_tasks(), if you need to output the primary and secondary differently.
MarkLNH

Drupal Site Map Module

I am looking for a module that can create sitemap in Drupal, but couldn't find any. I tried Site Map module, but it can only generate a sitemap page; it can't create a sitemap block at the end of every page. I also tried site menu module, but it can't create a sitemap block as shown above, as well.
Maybe it's just that I don't know how to configure, but I read every readme file, and tried for a few days, still can't get it to work.
Anyone has any idea?
I had the same problem, after trying a module (site-map) but missing customization options I wrote a custom module. Took less time then messing with the site-map module, for just getting a site map the following code is enough (adapt your-menu):
function sitemap_render_menu ($menu) {
$output = "<ul>";
foreach ($menu as $item) {
$link = $item["link"];
if ($link["hidden"]) {
continue;
}
$output .= "<li>" . $link["title"] . "</li>";
if ($item["below"]) {
$output .= sitemap_render_menu($item["below"]);
}
}
$output .= "</ul>";
return $output;
}
function sitemap_content () {
$output = "<h1>Sitemap</h1>";
$output .= "<span id=\"sitemap\">";
$output .= sitemap_render_menu(menu_tree_all_data("your-menu"));
$output .= "</span>";
return $output;
}
function sitemap_menu () {
$items = array();
$items["sitemap"] = array (
"title" => "Sitemap",
"page callback" => "sitemap_content",
"access arguments" => array("access content"),
"type" => MENU_CALLBACK);
return $items;
}
There is a basic comparison of sitemap modules at http://groups.drupal.org/node/15980
I have used sitemenu and it worked for my needs, but the real answer depends on how you structure your site with taxonomy, content types, etc.
Something like Auto Menu might work for you here as well. You could simply add the menu it generates to a footer block on your front page.
With the Site Map module installed, this php code will print the site map.
<?php echo theme('site_map'); ?>
You could create an empty views block and specify the above for the empty text, selecting the PHP Code input format.
There may be a better way to create a custom block to display php code, but I don't know it.
My idea here is to use Views module with customized block type.
I think you can benefit from Menu block module. as you can create menu blocks for all the menus you need in the footer. Then you can add them all in footer or in a one block using minipanels block (from panels module).
You can use Footer_sitemap module which provides us with the configurable block.
https://drupal.org/project/footer_sitemap
This is a little mod of best answer it uses current theme to show hierarchy
function sitemap_render_menu ($menu) {
$output = "<ul class='menu'>";
foreach ($menu as $item) {
$link = $item["link"];
if ($link["hidden"]) {
continue;
}
$cc=($item["below"]) ? "class='collapsed'" : '';
$output .= "<li $cc>" . $link["title"] . "";
if ($item["below"]) {
$output .= sitemap_render_menu($item["below"]);
}
$output .= "</li>";
}
$output .= "</ul>";
return $output;
}
function sitemap_content ($title,$menu) {
$output = "<h1>$title</h1>";
$output .= "<span id=\"sitemap\">";
$output .= sitemap_render_menu(menu_tree_all_data($menu));
$output .= "</span>";
return $output;
}
function sitemap_menu () {
$items = array();
$items["sitemap"] = array (
"title" => "Sitemap",
"page callback" => "sitemap_content",
"access arguments" => array("access content"),
"type" => MENU_CALLBACK);
return $items;
}
print sitemap_content("Navigational menu","Navigation");
A simple solution which doesn't depend on content being included as a menu item can be achieved by :
Creating a new view
Output as a block
Using Fields:
Content Title (configured to "Link this field to the original piece of content"
Content Type (configured to "Exclude from display")
Format as
Unformatted list with settings - Grouping field Nr.1 select Content:Type;
Filter Criterea:
Content: Published (Yes)
Content Type - Configure to select the content types you wish to include;
Sort criteria - Configure as per your preference

Resources