Adding custom menu item to WordPress menu - php

I have a WordPress menu that has a few menu items I added through the standard (drag and drop) WordPress admin menu feature. Recently I had to add another item to the menu that generates a dynamic href link. I achieved that using the following code in my functions.php file:
//add my profile menu item dynmacially to the members menu (generate
user name based on current user logged in)
add_filter('wp_nav_menu_items','add_profilelink_in_menu', 10, 2);
function add_profilelink_in_menu( $items, $args ) {
if( $args->theme_location == 'secondary') {
global $current_user;
//converts user id to username
$user_info = get_userdata($current_user->ID);
$items .='<li id="menu-item-2091" class="menu-item menu-item-2091">
Profile
</li>';
}
return $items;
}
My problem is that this menu item is added to the end of the menu and the regular WordPress Menu classes such as 'current-menu-item' don't get applied to this item. Is there a way for me to control the position of where this menu item is added to (For example: add this item after the first two items?)
and how can I get WordPress to treat this dynamically generated menu item as a regular menu item and have it add all the classes that it adds the other menu items (created through the WordPress menu feature)?
Thanks for any help.

Here's the logic that you can base using jquery
//suppose your menu is this
<ul id="secondary_nav">
<li id="li_unique_id_1">menu 1</li>
<li id="li_unique_id_2">menu 2</li>
<li id="li_unique_id_4">menu 4</li>
</ul>
//the jquery workaround
//place this in your footer
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'></script>
<script type='text/javascript'>
$(function(){
<?php
global $current_user;
//converts user id to username
$user_info = get_userdata($current_user->ID);
?>
$("<li id='menu-item-2091' class='menu-item menu-item-209'><a href='https://www.mysite.com/members/<?php echo $user_info->user_login; ?>'>Profile</a></li>").insertAfter("#secondary_nav #li_unique_id_2");
});
</script>
You can also use insertBefore function

Did you check Wordpress menu option in the themes->menu for this ? You can easily add menu from there also you can set custom menu from there.Hope it will help you.

Related

How to add <i> item if menu has sub-menu into wp_nav_menu

I have this type of menu, and I want this menu to convert into wp_nav_menu
<li>menu ab</li>
<li >menu bc<i class="hover-ind fa fa-angle-down" aria-hidden="true"></i></li>
<ul class="sub-menu">
<li>sub menu ab</li>
</ul>
As you can see if menu have submenu then I have to add <i></i> code snippets to show down arrow with particular menu.
So I am trying, if menu has sub menu then add <i></i> with anchor link into li.
I am able to add attributes to li if menu has submenu by this code:
add_filter( 'nav_menu_link_attributes', 'wpse154485_add_aria_haspopup_atts', 10, 3 );
function wpse154485_add_aria_haspopup_atts( $atts, $item, $args ) {
if (in_array('menu-item-has-children', $item->classes)) {
$atts['aria-haspopup'] = 'true';
}
return $atts;
}
but I need add a <i></i> code snippet if menu has submenu, Please help if anyone have clue about it.
TIA
you need to modify the Walker_Nav_Menu for adding your HTML markup.
check the GitHub link below for sample code.
https://github.com/wp-bootstrap/wp-bootstrap-navwalker
I have resolved this issue. May be if someone looking for answer in future then my solution can help them. here is I found a blog which is exactly for the solution for drop down indicator.
http://dksolution.in/add-dropdown-arrow-indicators-to-wordpress-menu-items-that-have-submenus/

Woocommerce use product tags to hide menu items using wp_nav_menu_objects filter

I am using a wordpress registered menu to display an unordered list of product tags on my product archives page.
I would like to maintain the hierarchy of an unordered list if possible, however, I would like to grey out the list items that have no products associated with them so users are not led to a page with no products.
the unordered list looks like this after wordpress does its thing, so each anchor has a title that is equal to the tag name:
<ul id="menu-themes" class="menu">
<li>
<a href='#' title='fantasy'>Fantasy</a>
</li>
<li>
<a href='#' title='science'>Science</a>
<ul class='sub-menu'>
<li>
<a href='#' title='space'>Space</a>
</li>
</ul>
</li>
</ul>
I used a filter to change the href of each anchor to be appropriate. This is the filter I used in order to change this specific menu's anchor links:
function change_menu( $items, $args ){
if( $args->theme_location == "themes" ){
foreach($items as $item){
global $wp;
$current_url = home_url(add_query_arg(array(),$wp->request));
$item->url = $current_url . '/?product_tag=' . $item->title;
}
}
return $items;
}
add_filter('wp_nav_menu_objects', 'change_menu', 10, 2);
I have gotten a list of all the relevant tags to print out by using:
function woocommerce_product_loop_tags() {
global $post, $product;
echo $product->get_tags();
}
Now let's say for example, this above function only echo's space. Is there a way to filter the menu further to hide all the menu items that do not equal space?
I'm thinking it would be something like this:
function filter_menu_by_tags($items, $args){
//set scope of $product variable
global $product;
//this if statement makes sure only the themes menu is affected.
if( $args->theme_location == "themes"){
//loop through each menu item
foreach($items as $item){
if($item->title does not match any of the tags in $product->get_tags()){
//then add a special class to the list item or anchor tag
}
else{
//do nothing and let it print out normally.
}
}
}
}
$product->get_tags() returns an array of tags. You can use PHP in_array() function to check if your title is on the list:
function filter_menu_by_tags($items, $args){
//set scope of $product variable
global $product;
//this if statement makes sure only the themes menu is affected.
if( $args->theme_location == "themes"){
//loop through each menu item
foreach($items as $item){
if( !in_array($item->title, $product->get_tags()) ){
// Title is not in_array Tags
//then add a special class to the list item or anchor tag
}
else{
// Title is in_array Tags
//do nothing and let it print out normally.
}
}
}
}

Missing Menu Item id from wordpress menu

I have just noticed that my menu items id are not getting generated by wordpress.
I expect it to be something like this
<li id='menu-item-2091' class='menu-item menu-item-209'>
<a href='www.mysite.com/members/<?php echo $user_info->user_login; ?>'>Profile</a></li>
but i get this without the menu item id.
<li class='menu-item menu-item-209'>
<a href='www.mysite.com/members/<?php echo $user_info->user_login; ?>'>Profile</a></li>
This error is occurring in my main menu, but my top menu seems to generate the menu item id properly.
I have looked around the menus area and can't find any clues why??
Here is a filter that hacks it out
add_filter ('wp_nav_menu_items','gfb_missing_id_fix', 10, 2);
function gfb_missing_id_fix($menu, $args) {
if($args->theme_location == "primary-menu"){
$dom = new DOMDocument;
$dom->loadHTML($menu);
foreach($dom->getElementsByTagName('li') as $element ) {
$classes = $element->getAttribute("class");
preg_match("/menu-item-\d+/", $classes, $output_array);
$element->setAttribute("id", $output_array[0]);
}
$menu = $dom->saveHTML();
}
return $menu;
}
General info
I use my navigation twice (mobile and desktop with different menu ids) and only the second menu is missing the item ids. This makes sense, because it secures that menu-item-ids are only used once.
JQuery Solution
For me the following javascript / jquery solution does the trick. It adds menu-id-ID (with ID as the actual menu-item-id) to each menu item.
var $mainMenu = $("#main-nav"); // your menu_id used in wp_nav_menu
$mainMenu.find("li").each(function(index, element) {
var $element = $(element);
var classes = $element.attr("class"),
ID = classes.match(/menu-item-(\d+)/)[1];
$element.attr("id", "menu-id-" + ID); // here 'menu-id-' is prepended to the actual ID
});

Adding dynamic Link to menu Item in wordpress

I want to have 'Text only' in menu of my wordpress website without plugin.So how to set dynamic link to menu item in wordpress?
try this one
add_filter('wp_nav_menu_items', 'menu_link', 10, 2);
function menu_link($items, $args) {
if ($args->menu == 'footer'){
$items .= '<li class="copyright ">Text Only</li>';
$items .= '<li class="copyright ">Print Page</li>';
}
return $items;
// return $items1;
}
You will have to recreate the complete menu walker to make it text only, or remove the anchor through jquery. But if you just want a menu item that doesn't link anywhere, create a normal link menu item, and instead of an actual link use #.

Can I show a list of contacts within category, on the 'List all contact categories' page in Joomla?

So in a nutshell, you have two options for displaying Contacts in Joomla:
Show all Joomla Contact Categories.
Show all Joomla Contacts in a single Category.
I want to use the first option, but merge a list underneath each Category showing the list of contacts within that category, and a link to their profile.
The simplest way I thought of this was to edit a template override of the file com_contact/categories/default_items.php
I found a point where I want the list to appear, and then copied and pasted the code from the Category view (that generates the list of contacts).
<ul>
<?php // Add list of contacts for each category
foreach ($this->items as $i => $item) : ?>
<li>
<a href="<?php echo JRoute::_(ContactHelperRoute::getContactRoute($item->slug, $item->catid)); ?>">
<?php echo $item->name; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
But I am assuming I can't just copy and paste, as there needs to be an extra node added to $this->items.
At the moment, no list is being generated, just the <ul> outside the foreach loop.. but also interestingly, the <li> and the <a> IS being generated.. but linking to the current page I'm on (Probably because $item->slug is still being seen as the category).
So can anyone point me in the right direction as to how to reference the contacts within a category? All I'm after is the name and the slug/URL.
UPDATE:
I saw this in the same file (default_items.php) and although I realise it's referring to child categories... would this be a place to start for the actual contacts within the categories?
<?php if (count($item->getChildren()) > 0) :?>
<div class="collapse fade" id="category-<?php echo $item->id;?>">
<?php
$this->items[$item->id] = $item->getChildren();
$this->parent = $item;
$this->maxLevelcat--;
echo $this->loadTemplate('items');
$this->parent = $item->getParent();
$this->maxLevelcat++;
?>
</div>
<?php endif; ?>
BUMP - Does anyone have any experience with this? Or being able to call individual contacts when viewing a category? How are they linked?
For Category view in file default_children.php after tag <li... add code:
<?php
// Get Category Model data
$categoryModel = JModelLegacy::getInstance('Category', 'ContactModel', array('ignore_request' => true));
$categoryModel->setState('category.id', $child->id);
$categoryModel->setState('list.ordering', 'a.name');
$categoryModel->setState('list.direction', 'asc');
$categoryModel->setState('filter.published', 1);
$contacts = $categoryModel->getItems();
?>
For Custom Fields add this after previus code:
JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
foreach($contacts as $contactItem) {
$currentContFields[] = FieldsHelper::getFields('com_contact.contact', $contactItem, true);
}

Categories