Get number of siblings for a wp_nav_menu element - wordpress

I am using a custom Walker function to customise the display of a menu in Wordpress.
To go one step forward I would like to have a different HTML markup for submenus with more than X elements.
Is there a way I can check the number of elements for a given item within the start_el function of the Walker Walker_Nav_Menu?

It's not the simplest thing using the Walker class, so I thought of a solution using DOMDocument to parse the menu HTML and count the submenus children after the menu has been built (using the wp_nav_menu_{$menu->slug}_items filter):
add_filter( 'wp_nav_menu_{MENU_SLUG}_items', function( $items, $args ) {
$dom = new DOMDocument();
$dom->loadHTML($items);
// get the .sub-menu elements
$submenus = $dom->getElementsByTagName('ul');
// for each one if it has children add a class
// like has-{num}-children
foreach ($submenus as $ul) {
if($ul->hasChildNodes()) {
// number of child nodes divided by 2
// to exclude the text nodes
$numChildren = $ul->childNodes->length / 2;
$class = "has-$numChildren-children";
$ul->setAttribute(
'class', $ul->getAttribute('class') . ' ' . $class
);
}
}
// save the changes
$html = $dom->saveHtml($dom->documentElement);
// remove html and body tags added by $dom->loadHTML
$items = preg_replace('~</?(html|body)>~', '', $html);
return $items;
}, 10, 2);
This should add a class has-{num}-children to every submenu with children (where {num} is the number of children), so that you can target whatever you want.
It can be easily changed to apply the class only to the submenus with a minimum number of children.
Remember to change {MENU_SLUG} with the slug of your menu.

Related

How do I iterate through Wordpress admin menu sub menus?

I can iterate through the top level Wordpress menu items to change the name but I now need to iterate through the sub menu items of that menu. I know I can do it using exact values $submenu['edit.php'][5][0] = 'New Label'; but it's for a plugin where the menu item may not be in the same position every time so that won't work.
I've got this code for the top level menus.
function _rename_top_level_menu( $original, $new ) {
global $menu;
foreach( $menu as $key => $value ) {
if( $original === $value[0]) {
$menu[$key][0] = $new;
}
}
}
Have you tried to use menu walker?
Although the Walker class has many uses, one of the most common usages by developers is outputting HTML for custom menus (usually ones that have been defined using the Appearance → Menus screen in the Administration Screens).

Drupal 8 / How do i add an extra-class / highlight a 'view item' in a view list if the currentnode is the same?

I show a list of teasers within a view.
This view is included for this content type.
So it shows on every page of this contenttype.
How do i add a class to the view item so i ccan make it active?
Depending of how are you render the view, you should use a preprocessor, example for a grid render
function MYTHEME_preprocess_views_view_grid(&$variables) {}
once you do that you can add some logic into the preprocessor and add the class according this logic
function MYTHEME_preprocess_views_view_grid(&$variables) {
$options = $variables['view']->style_plugin->options;
$item_num = 0;
$items = [];
// Iterate over each rendered views result row.
foreach ($variables['rows'] as $item) {
// Add item attributes
$item_attrs = [];
$item_attrs['class'] = "view-grid__item view-grid__item--{$item_num}";
$items[$item_num]['attributes'] = new Attribute($item_attrs);
// Add item.content
$items[$item_num]['content'] = $item;
// Increase, decrease or reset appropriate integers.
$item_num++;
}
// Add items to the variables array.
$variables['items'] = $items;
}
See more info here
https://api.drupal.org/api/drupal/core!modules!views!views.theme.inc/function/template_preprocess_views_view/8.2.x

Wordpress add link if item has submenu

I want to add a button for a responsive dropdown in a wordpress menu, which I can trigger with javascript.
For that I need a custom link with an icon or something like that after the a-tag but inside the li-tag.
How can I check a menuitem if it has a submenu and add a custom a-tag inside?
I've tried the walker and add_filter function, but it doesn't work.
Can anybody help me?
Thanks
If you add this to your function.php file, it will add class dropdown to all the parent menus with children (submenus). Then you can target .dropdown with javascript.
function menu_set_dropdown( $sorted_menu_items, $args ) {
$last_top = 0;
foreach ( $sorted_menu_items as $key => $obj ) {
// it is a top lv item?
if ( 0 == $obj->menu_item_parent ) {
// set the key of the parent
$last_top = $key;
} else {
$sorted_menu_items[$last_top]->classes['dropdown'] = 'dropdown';
}
}
return $sorted_menu_items;
}
add_filter( 'wp_nav_menu_objects', 'menu_set_dropdown', 10, 2 );

Is it possible to *optionally* override a theme in Drupal 6?

I want to override the theming of only one (custom) menu. I can do this with phptemplate_menu_tree() but - of course - it overrides the rendering of all menus.
I've tried returning FALSE (an obvious technique IMO) if the menu is not the specific one I want to override - but this doesn't cause the overridden theme function to be called.
My only alternative (when the menu is anything other than the specific one) is to call the overridden function from within phptemplate_menu_tree() - but this seems to defeat the whole point of the override system, since the default rendering function will be hard-coded therein.
I hope the explanation is clear, and any help is greatly appreciated - tks.
UPDATE
For the sake of future reference, I'll explain how I solved this.
First off, the menu rendering starts with this function in menu.module:
function menu_block($op = 'list', $delta = 0) {
$menus = menu_get_menus();
// The Navigation menu is handled by the user module.
unset($menus['navigation']);
if ($op == 'list') {
$blocks = array();
foreach ($menus as $name => $title) {
// Default "Navigation" block is handled by user.module.
$blocks[$name]['info'] = check_plain($title);
// Menu blocks can't be cached because each menu item can have
// a custom access callback. menu.inc manages its own caching.
$blocks[$name]['cache'] = BLOCK_NO_CACHE;
}
return $blocks;
}
else if ($op == 'view') {
$data['subject'] = check_plain($menus[$delta]);
$data['content'] = menu_tree($delta);
return $data;
}
}
If you only want to override how individual item (links) are rendered then you can use the theme system (there are loads of references on how do this) - but if you want complete control on how the entire menu tree is rendered (for example, wrapping the output in nested DIVs so it can be centred on the page) then there is no way to override menu_block().
Therefore, I removed the menu I wanted to render differently from the administer blocks page (site building->blocks) and rendered the menu directly in my page.tpl.php using code something like this: (angle brackets removed)
$m = menu_tree_page_data('my-menu-id');
$o = "DIV";
foreach($m as $k => $v){
$o .= "SPAN {$v['link']['title']} /SPAN";
}
$o .= "/DIV";
echo $o;
I hope this helps.
I've had mixed success doing template.php menu overrides to force CSS classes and ids or HTML into the output.
You could make use of Block Theme when enabling the menu as a block, but I've never tried it.
http://drupal.org/project/blocktheme
If you want to tackle the template way, here are the zen menu override funcitons...
function zen_menu_item_link($link) {
if (empty($link['localized_options'])) {
$link['localized_options'] = array();
}
// If an item is a LOCAL TASK, render it as a tab
if ($link['type'] & MENU_IS_LOCAL_TASK) {
$link['title'] = '<span class="tab">' . check_plain($link['title']) . '</span>';
$link['localized_options']['html'] = TRUE;
}
return l($link['title'], $link['href'], $link['localized_options']);
}
function zen_menu_local_tasks() {
$output = '';
if ($primary = menu_primary_local_tasks()) {
$output .= '<ul class="tabs primary clear-block">' . $primary . '</ul>';
}
if ($secondary = menu_secondary_local_tasks()) {
$output .= '<ul class="tabs secondary clear-block">' . $secondary . '</ul>';
}
return $output;
}
You could use sections module, or look at how it switches theme for certain menu-items.
what I did was register a new theme function in my template.php called primary_links (because I wanted to only customize this menu in certain way) created the function mytheme_primary_links() in my template.php refreshed the cache so Drupal would add my theme function to the system then changed theme function on primary_links from links to my custom theme function primary_links - this allows me to customize only this 1 menu - could you do this and hook into where ever to change the theme function being called for your links?
Chris

Wordpress sidebar classes

There any way i could add a class for the specific order of widget item? i.e. the class name would be widgetorder-1 (for the first appearing widget), widgetorder-2 (for the widget appearing in the 2nd order), etc. I looked into filters but wasn't sure how that worked.
// Set this to whatever number you'd like the ordering to start on.
$my_blog_widget_count = 0;
// put your blog sidebar here
$my_blog_sidebar_id = '';
function output_my_widget_info($a){
global $my_blog_sidebar_id, $my_blog_widget_count;
if($a[0]['id'] == $my_blog_sidebar_id){
global $my_blog_sidebar_id, $my_blog_widget_count;
$a[0]['before_widget'] = preg_replace( '/ class="widget /i', ' class="widget widget-'.$my_blog_widget_count.' ', $a[0]['before_widget'] );
$my_blog_widget_count++;
}
return $a;
}
add_filter('dynamic_sidebar_params','output_my_widget_info');
That should do it for you. Just stick that inside your theme's functions.php file and watch it work.

Resources