Changing menu <li> to <dd> with wp_nav_menu - php

I created a theme in HTML using Zurb Foundation and then integrated it into a WordPress theme.
The primary nav section in my theme, in its most basic form is:
<dl>
<dd></dd>
<dd></dd>
<dd></dd>
</dl>
I configured wordpress's menu call as such:
wp_nav_menu( array(
'theme_location' => 'primary',
'menu' => '',
'container' => '',
'container_class' => '',
'container_id' => '',
'menu_class' => 'menu',
'menu_id' => '',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'items_wrap' => '<dl id="tabnav" class="sub-nav">%3$s</dl>',
'depth' => 0,
'walker' => '') );
But in order to change the wrapping <li> that wordpress puts out to a <dd> I would need to write a custom walker.
I looked through some custom walkers and can see parts of the logic, but is creating a class every time I want to use custom html for a menu the only/most efficient way to go about it?

digwp provides an easier approach than the walker
create a function that builds the menu from scratch.
in your functions.php
// custom menu example # http://digwp.com/2011/11/html-formatting-custom-menus/
function clean_custom_menus() {
$menu_name = 'nav-primary'; // specify custom menu slug
if (($locations = get_nav_menu_locations()) && isset($locations[$menu_name])) {
$menu = wp_get_nav_menu_object($locations[$menu_name]);
$menu_items = wp_get_nav_menu_items($menu->term_id);
$menu_list = '<dl>' ."\n";
foreach ((array) $menu_items as $key => $menu_item) {
$title = $menu_item->title;
$url = $menu_item->url;
$menu_list .= "\t\t\t\t\t". '<dd>'. $title .'</dd>' ."\n";
}
$menu_list .= "\t\t\t". '</dl>' ."\n";
} else {
// $menu_list = '<!-- no list defined -->';
}
echo $menu_list;
}
and to call it in your theme:
<?php if (function_exists(clean_custom_menus())) clean_custom_menus(); ?>
so you would have to edit the $menu_name to your menu slug.
here's the source

Related

Is it possible to pass a nav widget in the Wordpress (Sidebar) new $args?

I've spent some time searching, but I haven't really found anything concrete in regards to passing new $args to a navigation widget. I did stumble across this post. However, I think the answer is a little overkill for what I'm trying to achieve.
To sum up the linked post it basically goes on to show how you could accomplish what I need, but only if an entirely new widget is created.
Specifically, I'm looking to either merge or overwrite the following
$args exclusively for a menu widget placed within a Wordpress sidebar;
wp_nav_menu( array $args = array(
'menu' => "header-quicklinks",
'menu_id' => "quicklinks",
'theme_location' => "sidebar-header"
) );
If possible I would like to pass the ID of the widget, in my case nav_menu-6; to the function and have the $args only apply to that menu specifically, this way I can touch up the code to target other menus should I have the requirement.
Currently tinkering with the following;
function widget_nav_args($args){
$menu = $args['menu'];
if($menu->term_id === "menu-quick-links") { // < Error: non-object.
return array_merge( $args, array(
'menu_class' => 'TESTING', // for testing.
// More settings here ...
) );
}
return $args;
}
add_filter('widget_nav_menu_args', 'widget_nav_args');
You are nearly there. The widget_nav_menu_args filter accepts more parameters than just the $args for the nav. You want to look at the widget arguments which is the 3rd paremeter. It would look something like this:
function widget_nav_args( $nav_menu_args, $nav_menu, $args, $instance){ // <- notice extra params..
if( $args['id'] === 'sidebarheader' ) { // < This is where we check if it's the right widget
return array_merge( $nav_menu_args, array(
'menu_class' => 'TESTING', // for testing.
// More settings here ...
) );
}
return $nav_menu_args;
}
add_filter('widget_nav_menu_args', 'widget_nav_args', 10, 4);
Notice I had to explicitly say how many arguments to pass to my filter function. Be sure to read through the documentation in the WP Codex here.
Hope that helps!
add_filter('widget_nav_menu_args', 'my_wp_nav_menu_args');
function my_wp_nav_menu_args($args) {
if (is_page(2) //Only target page 2
&& $args['theme_location'] === 'primary') { // Check and only target the primary menu
$args['menu'] = 'Menu for Profile';
}
return $args;
}
sample arguments are as below.
$arguments = array(
'menu' => '',
'container' => 'div',
'container_class' => '',
'container_id' => '',
'menu_class' => 'menu',
'menu_id' => '',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
'item_spacing' => 'preserve',
'depth' => 0,
'walker' => '',
'theme_location' => ''
);

Custom menu Wordpress: add attribute data-letters="link name"

I have a custom menu :
function mfn_wp_nav_menu() {
$args = array(
'container' => 'nav',
'container_id' => 'menu',
'menu_class' => 'menu',
'fallback_cb' => 'mfn_wp_page_menu',
'depth' => 5,
'link_before' => '<span>',
'link_after' => '</span>',
);
}
And the input in HTML is :
<li id="id" class="class">
<a href="#" data-hash="">
<span>Start</span>
</a>
</li>
But I want to add data-letters="Link name", so for example :
<li id="id" class="class">
<a href="#" data-hash="">
<span data-letters="Start">Start</span>
</a>
</li>
Any Idea ?
You can extend the Walker menu class like this:
class MyMenu extends Walker {
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$output .= sprintf( "\n<li><a href='%s'%s><span data-letters='%s'>%s</span></a></li>\n",
$item->url,
( $item->object_id === get_the_ID() ) ? ' class="current"' : '',
$item->title,
$item->title
);
}
}
And apply it to your menu:
wp_nav_menu(array(
'menu' => 2, // menu id
'walker' => new MyMenu()
// your other options
));
See this example from the reference: here

Create own footer menu in Wordpress?

I do have two menus in my Wordpress-Page which are called
Mainmenu(Primary Menu)
Footer
I associated the "Mainmenu" to the header of the website. I want the footer of the Wordpresspage to look exactly like the header, only difference should be the Links in it. It should not be taken from the "Mainmenu", but from the menu called "Footer". The function of the Header looks like this:
echo '<nav id="rtp-primary-menu" role="navigation" class="rtp-nav-wrapper' . apply_filters( 'rtp_mobile_nav_support', ' rtp-mobile-nav' ) . '">';
rtp_hook_begin_primary_menu();
/* Call wp_nav_menu() for Wordpress Navigaton with fallback wp_list_pages() if menu not set in admin panel */
if ( function_exists( 'wp_nav_menu' ) && has_nav_menu( 'primary' ) ) {
wp_nav_menu( array( 'container' => '', 'menu_class' => 'menu rtp-nav-container clearfix', 'menu_id' => 'rtp-nav-menu', 'theme_location' => 'primary', 'depth' => apply_filters( 'rtp_nav_menu_depth', 4 ) ) );
} else {
echo '<ul id="rtp-nav-menu" class="menu rtp-nav-container clearfix">';
wp_list_pages( array( 'title_li' => '', 'sort_column' => 'menu_order', 'number' => '5', 'depth' => apply_filters( 'rtp_nav_menu_depth', 4 ) ) );
echo '</ul>';
}
rtp_hook_end_primary_menu();
echo '</nav>';
The Theme i am using is rtPanel. Can anyone tell me how i simply can change this function in the way that it will load its content from the Menu called "Footer" and not from the Primary Menu which is "Mainmenu"?
Please dont worry about the CSS and everything, i will take care about this. I really just want to now how i have to change this function to load the "Footer"-Menu in it and not the Primary-Menu(Mainmenu).
As an Attachment how it look in the menu:
Thank you very much advance!
This is really simple, in fact when you start developing wordpress theme this might be one of the very first items you need to learn.
You need to register a menu location for footer seciton. You can learn more in codex
register_nav_menu( 'footer', __( 'Footer Menu', 'theme-slug' ) );
This will register the menu for you. Since you already have the menu location just find the location parameter. We're going to use that in the following code. For now assume it 'footer' like the above one. And the code for the footer menu according to your referenced code-
echo '<nav id="rtp-primary-menu" role="navigation" class="rtp-nav-wrapper' . apply_filters( 'rtp_mobile_nav_support', ' rtp-mobile-nav' ) . '">';
//rtp_hook_begin_primary_menu(); // we better disable this hook as it intends for the header primary menu
/* Call wp_nav_menu() for Wordpress Navigaton with fallback wp_list_pages() if menu not set in admin panel */
if ( function_exists( 'wp_nav_menu' ) ) {
wp_nav_menu( array( 'container' => '', 'menu_class' => 'menu rtp-nav-container clearfix', 'menu_id' => 'rtp-nav-menu', 'theme_location' => 'footer', 'depth' => apply_filters( 'rtp_nav_menu_depth', 4 ) ) );
} else {
echo '<ul id="rtp-nav-menu" class="menu rtp-nav-container clearfix">';
wp_list_pages( array( 'title_li' => '', 'sort_column' => 'menu_order', 'number' => '5', 'depth' => apply_filters( 'rtp_nav_menu_depth', 4 ) ) );
echo '</ul>';
}
//rtp_hook_end_primary_menu(); // we better disable this hook as it intends for the header primary menu
echo '</nav>';
Notice the theme_location parameter in wp_nav_menu() function? This is the one which plays behind to get you menu correctly.
Now play around with CSS and you might get what you intend.
Hope this might help you.

wp_menu in wordpress php function

I'm trying to create a simple nav in wordpress, however the wp_nav_menu parameters are ignored , am I missing something obvious?
I'm using a blank template called html5 blank. The Steps i've taken so are listed below
1) Register the menu in functions.php
2) From word press back end Create a menu called 'p' and assign pages
3) Give the menu a theme location
Registering and assigning menu location work fine , some but for some reason the wp_ parameters are ignored EG container , menu class, menu id etc...
If i inspect element, the container 'nav' is missing and the li items have default word press classes
BELOW is my code
Code in header.php
<?php html5blank_nav() ?>
Code in functions.php
function html5blank_nav()
{
wp_nav_menu(
array(
'theme_location' => 'primary pete',
'menu' => 'p'
'container' => 'nav',
'container_class' => '',
'container_id' => '',
'menu_class' => 'slimmenu',
'menu_id' => 'navigation',
'echo' => true,
'fallback_cb' => 'false',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'items_wrap' => '<ul>%3$s</ul>',
'depth' => 0,
'walker' => ''
)
);
}
// Register HTML5 Blank Navigation
function register_html5_menu()
{
register_nav_menus(array( // Using array to specify more menus if needed
'primary' => __('primary pete', 'Primary Menu'),
'sidebar-menu' => __('Sidebar Menu', 'html5blank'),
'extra-menu' => __('Extra Menu', 'html5blank')
));
}
// Remove the <div> surrounding the dynamic navigation to cleanup markup
function my_wp_nav_menu_args($args = '')
{
$args['container'] = false;
return $args;
}
// Remove Injected classes, ID's and Page ID's from Navigation <li> items
function my_css_attributes_filter($var)
{
return is_array($var) ? array() : '';
}
// Remove invalid rel attribute values in the categorylist
function remove_category_rel_from_category_list($thelist)
{
return str_replace('rel="category tag"', 'rel="tag"', $thelist);
}
// Add page slug to body class, love this - Credit: Starkers Wordpress Theme
function add_slug_to_body_class($classes)
{
global $post;
if (is_home()) {
$key = array_search('blog', $classes);
if ($key > -1) {
unset($classes[$key]);
}
} elseif (is_page()) {
$classes[] = sanitize_html_class($post->post_name);
} elseif (is_singular()) {
$classes[] = sanitize_html_class($post->post_name);
}
return $classes;
}
Many thanks,
P
When i try to reproduce this the menu with container_class => 'slimmenu' is shown.
Are you sure when you add a class to the container_class parameter this is not shown in the inspector?
Also noticed a missing comma after the parameter 'p' when i copied your code. But i don't think it will resolve your problem.

Navigational bar issue in WordPress

I am very new to PHP. I am handling a template which has been built and I am getting really confused. All I want to do is change the navigational bar names. Now normally with what I do it would be HTML but this seems to be defined in PHP and I am lost where to look.
My code is below I have targeted it down to where it is held as I have seen class in Google debugger. Now how can I find the list which is defined somewhere in my code. Which function do I go and search for next?
<nav class="site-navigation<?php echo esc_attr($menu_description); ?>">
<?php
$locations = get_theme_mod('nav_menu_locations');
/* Check if menu is selected */
$walker = '';
$menu = '';
$locations = get_theme_mod('nav_menu_locations');
if($locations && $locations['primary']) {
$menu = $locations['primary'];
if( (isset($_GET['page']) && $_GET['page'] == 'one-page') ) {
$menu = 21;
}
$walker = new description_walker();
}
wp_nav_menu( array(
'container' => false,
'menu_class' => '',
'echo' => true,
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'depth' => 0,
'walker' => $walker,
'menu'=>$menu
));
?>
<button class="fa fa-search desktop"></button>
</nav>
<?php
}
It looks like your issue is not in the code, as the above snippet is simply defining the menus for use through the WP admin.
Login to the back-end and visit Appearance > Menus and you should see a menu here, or the ability to create one.
Good Luck!

Categories