Modal on wp menu link - php

I'm trying to learn some new stuff but I stuck to something easy.
I managed to manipulate a link from wp menu using this code
add_filter( 'nav_menu_link_attributes', 'menu_item_data_toggle', 10, 2 );
function menu_item_data_toggle( $atts, $item) {
// Manipulate attributes
if ( 74 == $item -> ID )
$atts['data-toggle'] = 'modal';
return $atts;
}
but I need to use this modal with "if" attribute for login user and I can't figure how to.
The code for this modal I want to use is from a button
<?php
if( is_user_logged_in() ){
$account_manage = ''.classifieds_get_option( 'my_profile_looks' ).'';
$submit_ad = add_query_arg( array( $classifieds_slugs['subpage'] => 'submit_ad' ), classifieds_get_permalink_by_tpl('page-tpl_my_profile') );
$modal = '';
}
else{
$account_manage = ''.classifieds_get_option( 'login_looks' ).'';
$submit_ad = '#register';
$modal = 'data-toggle="modal"';
}
$locations = get_nav_menu_locations();
if ( isset( $locations[ 'top-navigation' ] ) ) {
wp_nav_menu( array(
'theme_location' => 'top-navigation',
'menu_class' => 'nav navbar-nav clearfix',
'container' => false,
'echo' => true,
'items_wrap' => '<ul class="%2$s">%3$s',
'depth' => 10,
'walker' => new classifieds_walker,
) );
}
if(get_option('users_can_register')){
echo '<li>'.$account_manage.'</li><li class="submit-add">'.esc_html__( 'SUBMIT AD', 'classifieds' ).'</li></ul>';
}
?>
Can you please me give me a hint to figure it out and use that menu link with this modal attributes?
Thank you!

Related

Function and Local variable doesn't work inside another function

Something is really strange or am I doing something wrong, I can't figure it out. When I add the variable $submenu in the wp_nav_menu args it doesn't work. See the examples below.
Error Message
Strict Standards: Only variables should be passed by reference in
This Works
$args = array(
'menu_id' => 'sidebar-menu',
'theme_location' => 'primary',
'submenu' => 'About Us'
);
wp_nav_menu( $args );
This doesn't work
$args = array(
'menu_id' => 'sidebar-menu',
'theme_location' => 'primary',
'submenu' => get_top_level_title()
);
wp_nav_menu( $args );
This function gets the top most parent title nothing fancy.
function get_top_level_title(){
global $post;
$category = get_the_category( $post->ID );
$parent_title = get_the_title( navz_top_most_parent_page_id() );
if( is_single() ){
$category = get_the_category( $post->ID );
$post->post_parent = $category[0]->term_id;
return get_cat_name($category[0]->category_parent);
} else if( $parent_title and !is_category() ){
return $parent_title;
} else {
$cat_obj = get_category( get_query_var( 'cat' ) );
$parent = $cat_obj->parent;
return get_cat_name($parent);
}
}
This code gets the function above into a local variable and passess it to the wp_nav_menu to get the submenu.
$submenu = get_top_level_title();
$args = array(
'menu_id' => 'sidebar-menu',
'theme_location' => 'primary',
'submenu' => $submenu
);
wp_nav_menu( $args );

Active sub-menu item not displaying on Wordpress with Bootstrap

Really struggling with this regarding sub-menus. I've made a custom theme for Wordpress using Bootstrap. I successfully managed to get to make my main nav menu dynamic with drop down sub menu links using wp_bootstrap_navwalker.
I've also made a nav-pills menu to display as a dynamic sub menu just below the main site nav menu and the page header. It shows all the child pages, of that particular parent.
My issue however, is that when you go on to a page with this menu, the active item on the menu does not display as active.
Here is my HTML/PHP:
<div class="nav-grey-band">
<?php
$args = array(
'menu' => 'primary',
'theme_location' => 'primary',
'submenu' => 'The Team',
'container_class' => 'centered-pills',
'menu_class' => 'nav nav-pills'
);
wp_nav_menu( $args );
?>
</div>
Here is what's in my functions.php:
// Register Custom Navigation Walker
require_once('wp_bootstrap_navwalker.php');
add_filter( 'wp_nav_menu_objects', 'submenu_limit', 10, 2 );
function submenu_limit( $items, $args ) {
if ( empty( $args->submenu ) ) {
return $items;
}
$ids = wp_filter_object_list( $items, array( 'title' => $args->submenu ), 'and', 'ID' );
$parent_id = array_pop( $ids );
$children = submenu_get_children_ids( $parent_id, $items );
foreach ( $items as $key => $item ) {
if ( ! in_array( $item->ID, $children ) ) {
unset( $items[$key] );
}
}
return $items;
}
function submenu_get_children_ids( $id, $items ) {
$ids = wp_filter_object_list( $items, array( 'menu_item_parent' => $id ), 'and', 'ID' );
foreach ( $ids as $id ) {
$ids = array_merge( $ids, submenu_get_children_ids( $id, $items ) );
}
return $ids;
}

Display current parent and its sub-menu only - wordpress

I have a main navigation and all parents have children.
Eg:
Page A: About Us
child1
child2
Page B : Our services
Child 3
Child 4
I need to include a horizontal sub-menu on a page. But my problem is, if currently we are on page A, all the child items of page A only to be displayed on page.
If we are on Page A, it should looks like:
Page A
Child 1
Child 2
Like this, when we go to Page B, the child of page B only to be displayed.
<?php
$args = array(
'theme_location' => '',
'menu' => '13', //nav menu id, which has about-us as a 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>',
'depth' => 0,
'walker' => ''
);
$menu_items = wp_nav_menu($args);//wp_get_nav_menu_items(13);
I tried writing above code, which resulted in all the parent items with their children.
Can someone help me on this?
In short, I want to get all the children(sub-menu) of About us menu entry.(i.e. I want child1 & child2 as a list with <a> tags)
Please write this code in theme's functions.php
<?php
// add hook
add_filter( 'wp_nav_menu_objects', 'my_wp_nav_menu_objects_sub_menu', 10, 2 );
// filter_hook function to react on sub_menu flag
function my_wp_nav_menu_objects_sub_menu( $sorted_menu_items, $args ) {
if ( isset( $args->sub_menu ) ) {
$root_id = 0;
// find the current menu item
foreach ( $sorted_menu_items as $menu_item ) {
if ( $menu_item->current ) {
// set the root id based on whether the current menu item has a parent or not
$root_id = ( $menu_item->menu_item_parent ) ? $menu_item->menu_item_parent : $menu_item->ID;
break;
}
}
// find the top level parent
if ( ! isset( $args->direct_parent ) ) {
$prev_root_id = $root_id;
while ( $prev_root_id != 0 ) {
foreach ( $sorted_menu_items as $menu_item ) {
if ( $menu_item->ID == $prev_root_id ) {
$prev_root_id = $menu_item->menu_item_parent;
// don't set the root_id to 0 if we've reached the top of the menu
if ( $prev_root_id != 0 ) $root_id = $menu_item->menu_item_parent;
break;
}
}
}
}
$menu_item_parents = array();
foreach ( $sorted_menu_items as $key => $item ) {
// init menu_item_parents
if ( $item->ID == $root_id ) $menu_item_parents[] = $item->ID;
if ( in_array( $item->menu_item_parent, $menu_item_parents ) ) {
// part of sub-tree: keep!
$menu_item_parents[] = $item->ID;
} else if ( ! ( isset( $args->show_parent ) && in_array( $item->ID, $menu_item_parents ) ) ) {
// not part of sub-tree: away with it!
unset( $sorted_menu_items[$key] );
}
}
return $sorted_menu_items;
} else {
return $sorted_menu_items;
}
}
Then you can display it in your theme using wp_nav_menu (just like you normally would), but also passing in a sub_menu flag to activate the custom sub_menu function:
<?php
wp_nav_menu( array(
'theme_location' => 'primary',
'sub_menu' => true
) );
?>
When on a page you first get all the pages, then you can get the current page ID, get the children in an array and loop through this array like this:
<?php
// First get all the pages in your site
$wp_query = new WP_Query();
$all_pages = $wp_query->query(array('post_type' => 'page'));
// Then get your current page ID and children (out of all the pages)
$current_page_id = get_the_id();
$current_page_children = get_page_children($current_page_id, $all_pages);
// Loop through the array of children pages
foreach ($current_page_children as $child_page) {
// Echo whatever you want from the pages
}
?>
EDIT: This has nothing to do with the structured menus you make in the backend, it has to do with making a page child of another page directly in the page edit section.
This is doing it all
<?php
global $wp_query;
if( empty($wp_query->post->post_parent) ) {
$parent = $wp_query->post->ID;
} else {
$parent = $wp_query->post->post_parent;
} ?>
<?php if(wp_list_pages("title_li=&child_of=$parent&echo=0" )): ?>
<div>
<ul>
<?php wp_list_pages("title_li=&child_of=$parent" ); ?>
</ul>
</div>
<?php endif; ?>
Thanks for all the solutions !
I wrote a function to help with this, since most examples I found include the page children, but not the parent itself. Just add this function to your functions.php file:
<?php
// Display sub menu
function the_sub_menu()
{
global $post;
// Open list
echo '<ul class="sub_menu">';
// Sub page
if($post->post_parent) {
// Load parent
$parent = get_post($post->post_parent);
// Add parent to list
echo '<li>' . $parent->post_title . '</li>';
// Add children to list
wp_list_pages('title_li=&child_of=' . $post->post_parent);
// Parent page
} else {
// Add parent to list
echo '<li class="current_page_item">' . $post->post_title . '</li>';
// Add children to list
wp_list_pages('title_li=&child_of=' . $post->ID);
}
// Close list
echo '</ul>';
}
Then, to use it on a page, simply call it like this:
<?php get_header() ?>
<?php while (have_posts()): the_post() ?>
<!-- Include the sub menu! -->
<?php the_sub_menu() ?>
<article>
<?php the_content() ?>
</article>
<?php endwhile ?>
<?php get_footer() ?>
First thing you need to do is to make your 'child1' 'child2' pages as child pages of 'About Us' page.
Click here to find out more on creating sub pages.
Once you have the pages structured, you can use this function, (link to docs)
<?php wp_list_pages( $args );
$args = array(
'depth' => 0,
'show_date' => '',
'date_format' => get_option('date_format'),
'child_of' => N, //N here should be replaced by your About-us page ID.
'exclude' => '',
'include' => '',
'title_li' => __('About Us'), //here you can mention any title you like for the list that's echoed by this function
'echo' => 1,
'authors' => '',
'sort_column' => 'menu_order, post_title',
'link_before' => '',
'link_after' => '',
'walker' => '',
'post_type' => 'page',
'post_status' => 'publish'
); ?>
Same goes for your 'Our services' page, Hope this resolves your problem. Do let us know if you face any problem & welcome to stackoverflow!
A fast and "dirty" solution.
I have created the following file .../wp-content/plugins/gabriel-submenu.php
With this content:
<?php
/**
* #package Gabriel_SubMenu
* #version 0.1
*/
/*
Plugin Name: Gabriel SubMenu
Plugin URI: http://www.nuage.ch
Description: My plugin to display a submenu
Author: Gabriel Klein
Version: 0.1
Author URI: http://www.nuage.ch
*/
function gab_submenu_content($a) {
$d = array(
'theme_location' => 'main_menu',
'child_of' => $a['id'],
'echo' => false,
'sort_column' => 'menu_order'
);
return wp_nav_menu( $d );
}
add_shortcode('gabsubmenu','gab_submenu_content' );
?>
Then in my posts I have:
[gabsubmenu id=93]
Where id is the id of the parent page.
Custom Menu and its Submenus
function thewebpixel_header_menu(){ ?>
<i class="fa fa-bars"></i>
<ul class="sf-menu fixed" id="menu">
<?php
$args = array(
'order' => 'ASC',
'orderby' => 'menu_order',
'post_type' => 'nav_menu_item',
'post_status' => 'publish',
'output' => ARRAY_A,
'output_key' => 'menu_order',
'nopaging' => true,
'update_post_term_cache' => false );
$items = wp_get_nav_menu_items( 'main', $args );
$parents = array();
foreach($items as $item )
$parents[] = $item->menu_item_parent;
function check_has_child($parents, $menu_id){
if(in_array($menu_id, $parents))
return "YES";
else
return "NO";
}
$flag = 0;
$count = 0;
foreach($items as $item ) { ?>
<?php if( !$item->menu_item_parent ){
if($count != 0 && $count != 5 && $flag == 2){
echo '</ul></div></div></li>';
$count=0;
$flag=0;
}
if(check_has_child($parents, $item->ID ) == "YES")
{
$liclass = '';
$aclass = 'class="sf-with-ul"';
}else{
$liclass = 'dropdown';
$aclass = '';
}
?>
<li class="<?php echo $liclass; ?>"><a <?php echo $aclass; ?> href="<?php echo $item->url;?>"><span><?php echo
$item->title;?></span></a>
<?php } ?>
<?php if( $item->menu_item_parent){ if($flag != 2)$flag = 1; ?>
<?php if($flag == 1) {
$flag = 2;
echo '<div class="sf-mega">';
}
if($count == 0 ){
echo '<div class="sf-mega-section"><ul>';
}
$count++;
?>
<li><a href="<?php echo $item->url; ?>">
<i class="fa fa-angle-right"></i>
<?php echo $item->title; ?>
</a>
</li>
<?php
if($count == 5){
echo '</ul></div>';
$count=0;
}
} ?>
<?php if( !$item->menu_item_parent && check_has_child($parents, $item->ID ) == "NO" ){ ?></li> <?php } ?>
<?php } ?>
</ul>
<?php } ?>
Find more information and examples at https://developer.wordpress.org/reference/functions/wp_get_nav_menu_items/ .

wp_nav_menu exclude pages from menu

I am trying to exclude pages from wp_nav_menu
Here is the function to exclude pages from menu
Works well when
<?php $page = get_page_by_title('my title' );
wp_nav_menu( array( 'theme_location' => 'menu-2', 'exclude' => $page->ID ) ); ?>
Works properly
BUt when using
<?php $page = get_page_by_title('my title' );
$pag1 = get_page_by_title('my title 1' );
$pag2 = get_page_by_title('my title2' );
wp_nav_menu( array( 'theme_location' => 'menu-2', 'exclude' => $page->ID,$pag1->ID,$pag2->ID ) ); ?>
Doesn't work properly.
try this
wp_nav_menu( array( 'theme_location' => 'menu-2', 'exclude' => '".$page->ID.",".$pag1->ID.",".$pag2->ID."' ) );
checkout this tutorial
Here is the correct solution
I hope it helps some other people
<?php
$page = get_page_by_title('my title' );
$pag1 = get_page_by_title('my title 1' );
$pag2 = get_page_by_title('my title2' );
$ids = "{$page->ID},{$pag1->ID},{$pag2->ID}";
wp_nav_menu( array( 'theme_location' => 'menu-2', 'exclude' => $ids ) );
?>
Managed it using the exclude method, but exclude by page id:
wp_nav_menu(
array(
'theme_location' => 'menu-2',
'exclude' => '599, 601'
)
);
(599 & 601 are page id's)
which is written here:
http://bacsoftwareconsulting.com/blog/index.php/wordpress-cat/wordpress-tip-how-to-exclude-pages-from-wp_nav_menu-function/
and finding the page id manually is written here:
http://bacsoftwareconsulting.com/blog/index.php/wordpress-cat/how-to-find-tag-page-post-link-category-id-in-wordpress/
It should work if you put the pages that you want to exclude into an array.
Check out this: http://ehsanis.me/2010/11/30/wordpress-wp_nav_menu-to-exclude-pages/
You can use a Custom Walker Function to just skip the rendering of the menu item.
wp_nav_menu( array(
'theme_location' => 'menu-2',
'walker' => new custom_navigation
) );
class custom_navigation extends Walker_Nav_Menu {
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
parent::start_el($item_html, $item, $depth, $args);
$exclude = array();
$exclude[] = get_page_by_title( 'my title' );
$exclude[] = get_page_by_title( 'my title 1' );
$exclude[] = get_page_by_title( 'my title2' );
if ( ! in_array( $item->object_id, $exclude ) ) {
// add your menu item html to the $output variable
}
}
}

How to Remove 'Home' Link from wp_nav_menu!

How do I get rid of the 'Home' link from appearing at the top of my links when using <?php wp_nav_menu( array('menu' => 'news', 'show_home' => false)); ?>
I tried 'show_home' => false and 'show_home=0' but neither worked.
This should be in your functions.php
function page_menu_args( $args ) {
$args['show_home'] = FALSE;
return $args;
}
add_filter( 'wp_page_menu_args', 'page_menu_args' );
EDIT: Dont forget to add this to wherever your menu is supposed to print out:
wp_nav_menu( array('echo'=>true));
The following worked for me:
_nav_menu( array( 'container_id' => 'topmenu', 'depth' => 0, 'menu_class' => 'sf-menu', 'theme_location' => 'topmenu' ) );
And I add
function page_menu_args( $args ) {
$args['show_home'] = FALSE;
return $args;
}
add_filter( 'wp_page_menu_args', 'page_menu_args' );
In the functions.php file.
If you are like me looking to remove the 'home' link from the default wordpress menu (wp_page_menu) and the home is a page (not blogposts), this is one way to solve it:
in functions.php:
function getPageBySlugname($slugname) {
$args = array(
'post_type' => 'page',
'hierarchical' => 0,
'post_status' => 'publish'
);
$pages = get_pages($args);
foreach ($pages as $page) {
if ($page->post_name == $slugname) {
return $page->ID;
}
}
}
in header.php
wp_page_menu(array(
'container' => 'div',
'show_home' => false, // Not sure what this is hiding, maybe if you have blogposts as home??
'echo' => true,
'exclude' => getPageBySlugname('homepage-slugname'), // change this to your slugname
));
You're making it way too hard! Instead, use CSS display: none for that particular .home item of a custom menu. It works like a charm. Example:
menu-blogroll .home {display:none !important;}
I used jquery to fix the same.
$("div.menu > ul li:first-child").css("display","none");

Categories