Shared multisite menu link to subsite - wordpress

I currently have a subsite set up with a shared navigation menu from the primary site. I'm using this function to switch to the main site whenever the menu function is called.
global $blog_id;
$current_blog_id = $blog_id;
function theme_nav()
{
switch_to_blog(1);
wp_nav_menu(
array(
'theme_location' => 'header-menu',
'menu' => '',
'container' => 'div',
'container_class' => 'menu-{menu slug}-container',
'container_id' => '',
'menu_class' => 'menu',
'menu_id' => '',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'items_wrap' => '<ul class="main-menu">%3$s</ul>',
'depth' => 0,
'walker' => ''
)
);
switch_to_blog($current_blog_id);
}
this is generally working but i'd like the links in the menu to be relative to the subsite, and not the primary site root. so site.com/subsite/products instead of site.com/products
i thought about writing a js script that updates the links when the page loads to insert the subsite into the link. is there a better way to do this?

Related

wp_nav_menu() overrides ul attributes

I have built WP templates before but I used to build them from scratch and I didn't have any issues like this. This time I decided to use html5blank boilerplate. Now I have this issue in the wp_nav_menu() where WordPress overrides the attributes I try to add in the items_wrap:
wp_nav_menu(
array(
'theme_location' => 'header-menu',
'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 class="nav navbar-nav">%3$s</ul>',
'depth' => 0,
'walker' => ''
)
);
The WordPress docs state that the items_wrap should be assigned properly as <ul id="%1$s" class="%2$s">%3$s</ul>. There's also this filter function below that should supposedly replace the string values but there are no docs that I could find whatsoever that show an example of how do I approach it.
function my_wp_nav_menu_args($args = '')
{
$args['container'] = false;
return $args;
}
Don't know if I got it right – do you want the ul element contain the classes "nav" and "navbar-nav"? Then you only need to change "menu_class" within your array:
wp_nav_menu(
array(
'theme_location' => 'header-menu',
'menu_class' => 'nav navbar-nav'
)
);
I solved this by removing item_wrap completely from the array and assigning in the menu_class .

How to implement menu navigation in admin panel same as shown in front?

I need to implement menu navigation shown in front same in backend in wordpress, need to show hierarchy of main page (as menu) and sub-pages as sub-menu in backend. I also searched for plugin also (https://wordpress.org/plugins/admin-collapse-subpages/ and https://wordpress.org/plugins/cms-tree-page-view/) but these plugins are also not fulfilling the required behaviour. Can anyone please help me on this.
// create page in admin panel
add_action('admin_menu', function(){
add_menu_page( 'Some admin', 'Admin page', 'manage_options', 'site-options', 'add_my_setting', '', 4 );
} );
function add_my_setting(){
// get the menu
wp_nav_menu( array(
'theme_location' => '',
'menu' => 'main_menu',
'container' => 'div',
'container_class' => 'some-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' => '',
) );
}
// and add you styles for menu with some-container-class
add_action('admin_enqueue_scripts', function(){
wp_enqueue_style( 'myPluginStylesheet', plugins_url('stylesheet.css', __FILE__) );
});

current page style in wordpress with html 5 blank

anybody knows the blank theme html5 for wordpress? well i'm using this theme and i'm trying to add some style to the current page but i don't know how.
this is the function that the theme has:
function html5blank_nav()
{
wp_nav_menu(
array(
'theme_location' => 'header-menu',
'menu' => '',
'container' => 'div',
'container_class' => 'menu-{menu slug}-container',
'container_id' => '',
'menu_class' => 'menu',
'menu_id' => '',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'items_wrap' => '<ul>%3$s</ul>',
'depth' => 0,
'walker' => ''
)
);
}
thank you very much!
See your question is not clear enough!! The above code is for styling the nav menus,
Here you can change the menu class, the container and lot more!!

Add sub text in the wordpress menu

I want to add text here within the anchor tag of the wordpress menu. So the menu structure would be
<ul>
<li><a href="#">Item1<br>
<span class="sub-text">text here<span></a>
</ul>
The "Item1" and "text here" will be dynamic. That is it can be edited from the wordpress back end.
I am using the wordpress function wp_nav_menu to show the menu. Below is the code.
$defaults = array(
'theme_location' => 'primary',
'menu' => '',
'container' => false,
'container_class' => '',
'container_id' => '',
'menu_class' => 'nav navbar-nav',
'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' => -1,
'walker' => ''
);
wp_nav_menu( $defaults );
Please help.
You need a custom Walker to achieve that:
$walker = new Menu_With_Description;
wp_nav_menu( array(
'theme_location' => 'primary',
'walker' => $walker
) );
Tutorials all over the place on the web, e.g.: http://www.wpbeginner.com/wp-themes/how-to-add-menu-descriptions-in-your-wordpress-themes/

WordPress menu strange issue

While customizing the wordpress menu I am having serious issues.
$setup = array( 'menu' => '', 'container' => 'div', 'container_class' => 'menu', 'container_id' => 'menu', 'menu_class' => 'menu', 'menu_id' => 'menu',
'echo' => true, 'fallback_cb' => 'wp_page_menu', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '', 'items_wrap' => '<ul id="%1$s">%3$s</ul>',
'depth' => 0, 'walker' => '', 'theme_location' => '','theme_location' => 'primary' );
wp_nav_menu($setup);
The issue:
You can see in the code that I have setted the menu_id as "menu". OR 'menu_id' => 'menu'
But it is not applying, only the menu_class is applying the id is not for the menu container. But why???? Please help me
If you are trying to apply css styles check if there is !important keyword in front of any style property.

Resources