Adding elements inbetween navigation links - php

I have the following code which makes/inputs my navigation into my site. The links are created through the admin panel. I want to add a '|' separator in between the links
HTML
<?php wp_nav_menu( array( 'container_class' => 'main-nav', 'theme_location' => 'primary' ) ); ?>
functions.php
/* Register 'primary' navigation */
function wptutsplus_register_theme_menu() {
register_nav_menu('primary', 'Main Navigation Menu');
}
add_action('init', 'wptutsplus_register_theme_menu');
Current output
Link 1 Link 2 ...
Desired output
Link 1 | Link 2 ...
html
<div class='col-md-6'>
<?php wp_nav_menu( array( 'container_class' => 'main-nav', 'theme_location' => 'primary' ) ); ?>
</div>

Updated v2
You can use after parameter of wp_nav_menu(), with a small CSS trick.
Here is the code:
$menuParameters = [
'container_class' => 'main-nav',
'theme_location' => 'primary',
'menu_class' => 'my_custom_class', //<-- add this
'after' => '<span class="sep">|</span>' //<-- add this
];
wp_nav_menu($menuParameters);
Add this to you style.css
.my_custom_class li:last-child .sep{ display:none; }
Alternative way (with pure PHP approach)
$menuParameters = [
'container_class' => 'main-nav',
'theme_location' => 'primary',
'after' => '|', //<-- add this
'echo' => false //<-- add this
];
$nav_html = wp_nav_menu($menuParameters);
$needle = isset($menuParameters['after']) ? $menuParameters['after'] : '';
$index = strrpos($nav_html, $needle);
if ($index)
{
echo substr_replace($nav_html, '', $index, strlen($needle));
}
else
{
echo $nav_html;
}
All code is tested and works.
Hope this helps!

add the symbol |(as many as menu links you have) as u would add normally a custom link in the wordpress menu.
after that get the class or id of every element with devtools then apply this css to every one of the elements
.itemtofind{
pointer-events: none;
cursor: default;
}
you can customize the css element as you wish , add padding, color, etc.
in the past i made a web page with that feature and everything was right.
when you are in mobile apply the correct media query and hidde the symbols | with this css to the elements
display:none

I'm not super sure how wordpress styles their links, but this should emulate that, I don't think simply inserting the textnodes will work.
.wp-navigation {
border-right:2px solid black;
}
.wp-navigation:last-child {
border-right:none;
}

Use CSS. Something like this should work.
.main-nav a:after {
content: '|';
display: inline-block;
margin: 0 .5em;
}

Related

How do I add fontawsome icon to wp_nav_menu?

How do I add fontawsome icon to footer menu i.e <ul><li><i class="fa-solid fa-arrow-up-right-from-square"></i> Category 1</li><ul> in a
<?php
if ( has_nav_menu( 'business-menu' ) ) :
wp_nav_menu( array(
'theme_location' => 'business-menu',
'items_wrap' => '%3$s',
'add_li_class' => 'list-group-item d-flex justify-content-between align-items-center border-0',
'container' => '',
));
endif;
?>
above is what have tried but stacked, anyone to help?
I would advice not to include the whole library for just few icons, that definitely slows down the site a bit.
-- you would need just 5-10 icons and the best solution would be to use the css and attach the svg's to classes, and add up the class to individual nav items like.
<ul><li><i class="social"></i> Category 1</li><ul>
.social::before {
content: '';
background-image: url("data:image/svg+xml,<svg viewBox='0 0 16 16' fill='%23333' xmlns='http://www.w3.org/2000/svg'><path fill-rule='evenodd' d='M8 9.5a1.5 1.5 0 1 0 0-3 1.5 1.5 0 0 0 0 3z' clip-rule='evenodd'/></svg>");
background-repeat: no-repeat;
background-size: 1rem 1rem;
/* set positioning and others */
}
You can get tons of svgs here: https://icons.getbootstrap.com/
and you can use the svgs in your html file as well, but just rendering them in a span tag.

How to add text under Font Awesome icons of handheld footer bar of Storefront?

I am configuring Storefront theme. The handheld footer bar of Storefront shows links on mobile devices. However, now, it just shows Font Awesome icons (not include text). How to include text under Font Awesome icons in this case?
I have tried to edit some codes, but the search bar appeared as an error in handheld footer bar of storefront.
Php codes added to functions.php:
add_filter( 'storefront_handheld_footer_bar_links', 'jk_add_home_link' );
function jk_add_home_link( $links ) {
$new_links = array(
'home' => array(
'priority' => 10,
'callback' => 'jk_home_link',
),
);
$links = array_merge( $new_links, $links );
return $links;
}
function jk_home_link() {
echo '' . __( 'Home' ) . '';
}
CSS:
.storefront-handheld-footer-bar ul li.home > a:before {
content: "\f015";
}
The text “Home” will not be displayed if I use above codes. How to display it under the Font Awesome icon?
Your code is fine but the text is hidden by this property decleration:
.storefront-handheld-footer-bar ul li > a {
text-indent: -9999px;
}
play around with the indent to position it. Not tried.
Same question answered by myself on https://stackoverflow.com/a/68511882/11225860, coping it from there.
The text is hidden due to "text-indent: -9999px;" so you can change it as follows.
Here I am additionally added "line-height:" to arrange position of the text.
.storefront-handheld-footer-bar ul li > a {
text-indent: 0px;
line-height: 95px;
}
If you need to adjust position of the icons, you can add following css and make changes on the value.
.storefront-handheld-footer-bar ul li>a::before {
line-height: 2;
}

Align custom created menu in Wordpress Footer vertically

I created custom menu into my Wordpress site. I registered the new menu into functions.php file using this code:
// Add new Footer menu
function register_my_menu() {
register_nav_menu('new-menu',__( 'New Footer Menu' ));
}
add_action( 'init', 'register_my_menu' );
and after that inserted this line into footer.php file from current theme:
<?wp_nav_menu( array( 'theme_location' => 'new-menu', 'container_class' =>
'new_menu_class' ) ); ?>
and menu is showing into footer, but its showing into list view, and I want to show vertically inline in footer, of course centered is possible. I used CSS to add inline styling like this:
.new_menu_class {
display:inline-flex;
}
But seems do not many any changes to menu in footer. Any help here?
You're applying styles to the container of the wp_nav_menu function where the structure of the returned html is
<div class="new_menu_class"> <ul> <li>...
'container_class'
(string) Class that is applied to the container. Default 'menu-{menu slug}->container'.
https://developer.wordpress.org/reference/functions/wp_nav_menu/
For your CSS to be applied on the list items (I'm assuming you wish to have the horizontally). you will need to add this class under the arguments for 'menu_class'
'menu_class'
(string) CSS class to use for the ul element which forms the menu. Default 'menu'.
If you applied the same class in your question to the menu_class you could apply the following:
.new_menu_class {
display: flex;
align-items: stretch;
justify-content: space-evenly;
width: 80%;
margin: 0 auto;
padding: 0;
}
Looking at your site I can see you have a set width container with media queries, I would suggest the same when applying this so as your UI is clear when viewing on smaller devices.

Can't apply css class to dynamic menu

The whole project can be found here Github.
I added a dynamic menu on my navigation bar placed in the header. For some reason I can't figure out how to link the dynamic menu with either a class or an ID defined in my style.css file.
For some reason, neither the 'menu_class' or the 'menu_id' seem to work.
Here the menu in the header.php file:
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<!--Container of my link that are on the far right, they collapse with a small screen-->
<ul class="nav navbar-nav navbar-right">
<!--This code inject the dynamic menu inside thge av bar-->
<!-- The Dynamic menu is managed in the admin section of word press-->
<?php
wp_nav_menu(
array(
/*must have this link to function.php
*In function.php we defined this menu 'alphamenu'*/
'theme_location' => 'top-right-menu',
/*this line of code removes the default menu appearence*/
'container' => false,
/*this line makes the menu with the same layout specified above
*(same as link 1 and 2)*/
'items_wrap' => '%3$s',
/*CSS class applied to the menu*/
'menu_class' => 'nav navbar-nav custom-head-foot',
'menu_id' => 'custom-dynamic-menu'
)
);
?>
</ul> <!--dynamic menu unordered list-->
</div> <!--dynamic menu div-->
The function.php file:
/*Function to register a custom menu for the admin section
*This resurces was taken from the wordpress codex*/
function custom_theme_setup() {
/*Registered a custom primary navigation menu*/
register_nav_menus (
array( 'alphamenu', __( 'Primary Menu 123') )
);
/*Add theme support for title tag*/
add_theme_support( 'title-tag' );
}
/*Hooking in the function "custom_theme_setup()" after the theme is setup
*surce: wordpress plugin api hooks*/
add_action( 'after_setup_theme', 'custom_theme_setup');
If the render method is screwed up you can always use .nav > div with or without pseudo-class like :nth-of-type(1) or there's second way: .nav:nth-child(1) as query for first child inside div.nav?
If you want to add class, try to add it within wp_nav_menu() you wrote like this:
wp_nav_menu(
array(
/*must have this link to function.php
*In function.php we defined this menu 'alphamenu'*/
'theme_location' => 'top-right-menu',
/*this line of code removes the default menu appearence*/
'container' => false,
/*this line makes the menu with the same layout specified above
*(same as link 1 and 2)*/
'items_wrap' => '%3$s',
/*CSS class applied to the menu*/
'menu_class' => 'nav navbar-nav custom-head-foot **add_your_class_within_single_quotes**',
'menu_id' => 'custom-dynamic-menu'
)
);
or you can apply selectors I wrote in first answer to your CSS.

wordpress drop down menu are not properly styled as they are inheriting style from parent . want to give custom background to drop downs

this is my code for navigation ----->
$main = array( 'theme_location' => 'header-menu' , 'link_before' => '<span>',
'link_after' => '</span>','container' => '',
'items_wrap' => '%3$s',
);
wp_nav_menu( $main );
i am applying span tag to style li items . what i actually want to do is that span tag should only be applied to main menu (depth = 1 in hierarchy ) and not to its sub menus(in drop down)
html ouptut is something like
<ul class=main>
<li><span>Testimonials</span></li> >
<li><span>Testimonials</span></li> >
<ul class ="sub=menu" >
<li><span>Testimonials</span></li> >
</ul>
<li><span>Testimonials</span></li> >
</ul>
i dont want span tag in sub-menu class li items, is there a way to do this .?
If you want to style the sub-menu differently, the identifier you should be using is .sub-menu but depending on the theme you are using, the author for the theme might have styled either the UL, LI, or A of the menu. Find that out from style.css and write your code for the same identifier.
eg. if style.css contains something like this
.menu li a { background: #fff; }
then you'll need to write this line:
.sub-menu { background: #888; }
It's also a good practice to use background if the theme's style used 'background', and 'background-color' if the theme's style used background-color.

Categories