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

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__) );
});

Related

Shared multisite menu link to subsite

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?

How to style <?php html5blank_nav(); ?> in functions.php

I've used a HTML 5 Blank template and converted it to a WordPress theme successfully.
But when I use this code: <?php html5blank_nav(); ?> to auto populate the main navigation it of course looses the template styling.
I've enqueued all the css but from what I understand I need to use the below code to tell the menu how to be styled:
PHP:
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' => ''
)
);
}
If you take a look at http://geekcloud.co.uk - this is the header navigation menu style im trying to achieve using this code: .
Any help would be greatly appreciated.
Thank you.
You might want to look into the wp_enqueue_style page on the Wordpress developer documentation.
You have to register the stylesheet in your functions.php file with
wp_enqueue_style( $handle, $src, $deps, $ver, $media );
Good luck developing!

Menu gets wrap in Div - wordpress

Working on Wordpress menu: with Bootstrap
My ul li gets wrap within Ul and creates Div.
Screenshot attached:
It seems it automatically adds classes to menu li and and prevents the bootstrap styles and classes
My wp menu function :
/**
* Get our wp_nav_menu() fallback, wp_page_menu(), to show a home link.
*/
function register_my_menus() {
register_nav_menus(
array(
'header-menu' => __( 'Header Menu' ),
'extra-menu' => __( 'Extra Menu' )
)
);
}
add_action( 'init', 'register_my_menus' );
Try changing in the theme template the nav menu function like this :
<?php
$defaults = array(
'theme_location' => '',
'menu' => '',
'container' => '', // <- This empty = no 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' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
'depth' => 0,
'walker' => ''
);
wp_nav_menu( $defaults );
?>
Source : https://codex.wordpress.org/Function_Reference/wp_nav_menu
Take a look at the container index of the defaults array.

Where/What is the wordpress function that outputs the menus?

Looking at the Wordpress code I am struggling to find where the menus are been put together, (to be rendered).
For example, if I want to output 'menu-1' where is the function/class that creates the output result?
Does it use a class to do the creation? Does it follow a specific pattern?
Walker_Nav_Menu And wp_nav_menu is behind the wordpress Navigation. According to your requirement you need to customize the wp_nav_menu
$defaults = array(
'theme_location' => '',
'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' => ''
);
wp_nav_menu( $defaults );
For adding the custom menu option that allows users to add and organize menus, you'll add this in your function.php
add_theme_support( 'menus' );
And then you can paste this code anywhere you want to display the custom menu
<?php wp_nav_menu( array( 'sort_column' => 'menu_order', 'container_class' => 'menu-header' ) ); ?>
the wp_nav_menu is the main function that displays menus on the WordPress page with a few arguments stored as an array. Sort_column instructs WordPress to follow the order in the options, and the container_class :.menu-header is the CSS class to style this menu. For displaying multiple menus, you can specify the id, slug, menu name with $id, $slug, $menu

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!!

Resources