I am trying to figure out the best method for moving my logo into the center of the main nav which is an unordered list (preferably as a new list item), at sizes above 980px.
I'm using a WordPress theme called Understrap, which is similar to the blank Underscores theme with Bootstrap baked-in. It uses wp-bootstrap-navwalker for navs, and the logo is uploaded through customizr (the client needs to be able to change the logo through the customizr UI, and set menu items through the dash, so a static solution isn't really do-able).
I have a solution, but I am not sure if it's the best way to go. Something definitely FEELS wrong about it.
The method I used was to move div#logo-wrapper from the parent div, into the main nav "ul", using 'insertAfter' with jQuery.
After that, I added a function that refreshes the browser at window resize, because simply appending/prepending, or using insertAfter doesn't work when the user resizes their browser.
My jQuery
jQuery(document).ready(function() {
if($(window).width() > 980)
{ jQuery("#logo-wrapper").insertAfter(".navbar-nav li:eq(2)");}
else{
}
});
$(window).bind('resize', function(e)
{
if (window.RT) clearTimeout(window.RT);
window.RT = setTimeout(function()
{
this.location.reload(false); /* false to get page from cache */
}, 100);
});
My Logo PHP
In my header.php file I wrapped all of the site's branding php in a div called "#logo-wrapper"
<div id="logo-wrapper">
<button class="dev-navbar-toggler-button navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
<span class="dev-navbar-toggler-icon navbar-toggler-icon"></span>
</button>
<?php if ( ! has_custom_logo() ) { ?>
<?php if ( is_front_page() && is_home() ) : ?>
<h1 class="dev-navbar-brand navbar-brand mb-0"><a rel="home" href="<?php echo esc_url( home_url( '/' ) ); ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>"><?php bloginfo( 'name' ); ?></a></h1>
<?php else : ?>
<a class="dev-navbar-brand-lnk navbar-brand" rel="home" href="<?php echo esc_url( home_url( '/' ) ); ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>"><?php bloginfo( 'name' ); ?></a>
<?php endif; ?>
<?php } else {
the_custom_logo();
} ?>
</div>
By doing this I can successfully move my div#logo-wrapper that wraps my logo options, link, image, and fallback text into the Main Nav's "ul", after the 3rd "li". I moved the HTML element this way, because I couldn't quite nail it with CSS. Basically every positioning technique I tried seemed more janky than manipulating the DOM (I could be wrong, but I tried every CSS trick could think of).
First problem:
As you can see I am still basically moving the entire div#logo-wrapper that I wrapped my php with, into the "ul" because I couldn't figure out how to move the logo link into a newly created "li", after the 3rd "li".
It would be nice to at least move it into a new "li" instead of it being moved by itself.
Second problem:
I would like to figure out a way to move the div#logo-wrapper to the center of the "ul", no matter how many new list items are added, instead currently moving it into place after the 3rd "li". I was thinking of trying this with the use of a modulus or something mathematical with JS.
Third Problem:
I really don't like that I have the browser refreshing every time the page is resized, that will get annoying. I just need the script to fire at only sizes above 980px, and then go back to the way it was below 980px. Just like a normal CSS media query.
CSS Issues vs. JS Issues
I only want this to happen at desktop, so it seems like CSS would be the way to go, but I could never get it to sit as perfect as this does when it's apart of the list. With CSS, the biggest problems were that the positioning would slip, or it wouldn't look the same in all browsers, or if a new menu item was added by the client, the whole menu would slip and be off, making the whole nav look and act poorly. So JS seemed like the cleanest way to do this while keeping everything in tact.
Question:
To simplify my question, I know there has to be simple way to achieve this. It's too common of a problem, but I haven't found any definite answers online for specifically achieving this. Does anyone know of a simple way to do this with either Javascript, CSS, or just through Nav Walker with PHP.
My Full Header PHP
<?php
/**
* The header for our theme.
*
* Displays all of the <head> section and everything up till <div id="content">
*
* @package understrap
*/
$container = get_theme_mod( 'understrap_container_type' );
?>
<!DOCTYPE html>
<!--[if IE 6]>
<html id="ie6" <?php language_attributes(); ?>>
<![endif]-->
<!--[if IE 7]>
<html id="ie7" <?php language_attributes(); ?>>
<![endif]-->
<!--[if IE 8]>
<html id="ie8" <?php language_attributes(); ?>>
<![endif]-->
<!--[if !(IE 6) | !(IE 7) | !(IE 8) ]><!-->
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-title" content="<?php bloginfo( 'name' ); ?> - <?php bloginfo( 'description' ); ?>">
<link rel="profile" href="http://gmpg.org/xfn/11">
<link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>">
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<div class="hfeed site" id="page">
<!-- ******************* The Navbar Area ******************* -->
<div class="dev-nav-wrapper wrapper-fluid wrapper-navbar" id="wrapper-navbar">
<a class="dev-scrn-skpcntnt-rdr-lnk skip-link screen-reader-text sr-only" href="#content"><?php esc_html_e( 'Skip to content',
'understrap' ); ?></a>
<nav class="dev-navbar-tggl navbar navbar-toggleable-md navbar-inverse bg-inverse">
<?php if ( 'container' == $container ) : ?>
<div class="dev-nav-container container">
<?php endif; ?>
<div id="logo-wrapper">
<button class="dev-navbar-toggler-button navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
<span class="dev-navbar-toggler-icon navbar-toggler-icon"></span>
</button>
<!-- Your site title as branding in the menu -->
<?php if ( ! has_custom_logo() ) { ?>
<?php if ( is_front_page() && is_home() ) : ?>
<h1 class="dev-navbar-brand navbar-brand mb-0"><a rel="home" href="<?php echo esc_url( home_url( '/' ) ); ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>"><?php bloginfo( 'name' ); ?></a></h1>
<?php else : ?>
<a class="dev-navbar-brand-lnk navbar-brand" rel="home" href="<?php echo esc_url( home_url( '/' ) ); ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>"><?php bloginfo( 'name' ); ?></a>
<?php endif; ?>
<?php } else {
the_custom_logo();
} ?><!-- end custom logo -->
</div>
<!-- The WordPress Menu goes here -->
<?php wp_nav_menu(
array(
'theme_location' => 'primary',
'container_class' => 'collapse navbar-collapse',
'container_id' => 'navbarNavDropdown',
'menu_class' => 'navbar-nav',
'fallback_cb' => '',
'menu_id' => 'main-menu',
'walker' => new WP_Bootstrap_Navwalker(),
)
); ?>
<?php if ( 'container' == $container ) : ?>
</div><!-- .container -->
<?php endif; ?>
</nav><!-- .site-navigation -->
</div><!-- .wrapper-navbar end -->
Desktop & Mobile Screenshot of Current Nav
[Desktop Nav][1]
[1]: https://i.stack.imgur.com/ulueF.jpg
[Mobile Nav][1]
[1]: https://i.stack.imgur.com/aAhLp.jpg