How to properly implement a structured menu in php - php

my goal is to tweak/replace the existing wordpress php menu code, having as a start point, this html/css/js menu structure in codepen:
Home / Portfolio / About and going down in Portfolio structure, another page having Home / Portfolio projects.
In a few words, I've registered in script-calls.php the mynewmenu.js file with the following sequence
js:
// mynewmenu implementation
jQuery(function($){
var height, index, prevIndex
$('nav ul li').mouseover(function(e){
//Set the aesthetics (similar to :hover)
$('nav ul li').removeClass('hovered');
$(this).addClass('hovered');
//Gets relevant data required for scrolling of menuRight
height = $("#menuRight").css("height").replace('px','');
index = $(this).index();
//If the category button (from the navlist) has changed
if (index != prevIndex){
$("#menuRight").stop();
$("#menuRight").animate({"scrollTop":height*index}, 800, 'easeOutBounce'); //This requires jQuery UI for easing options.
prevIndex = index;
}
});
});
I've created the desired menu structure (assigning different menus to different pages as u'll see here) and I've identified the php that manage the existing menu structure:
<!-- Start Header -->
...
<div class="myrow max_width ">
<div class="small-5 <?php if ($header_style == 'style2') { echo 'medium-8'; } else { echo 'medium-4';} ?> columns menu-holder">
<?php $full_menu_true = ($menu_mobile_toggle_view == 'style2' && $header_style == 'style2');?>
<?php if ($full_menu_true) { ?>
<nav id="full-menu" role="navigation">
<?php if ($page_menu) { ?>
<?php wp_nav_menu( array( 'menu' => $page_menu, 'depth' => 2, 'container' => false, 'menu_class' => 'full-menu', 'walker' => new thb_mobileDropdown ) ); ?>
<?php } else if(has_nav_menu('nav-menu')) { ?>
<?php wp_nav_menu( array( 'theme_location' => 'nav-menu', 'depth' => 2, 'container' => false, 'menu_class' => 'full-menu', 'walker' => new thb_mobileDropdown ) ); ?>
<?php } else { ?>
<ul class="full-menu">
<li>Please assign a menu from Appearance -> Menus</li>
</ul>
<?php } ?>
/* I think that <div id='menuRight'> html sequences *translated*
in *php* should begin here in a conditional manner: *if* we find ourself on the
Home page, should be activated Home / Portfolio / About sequence and also if we
are on the Portfolio page, we should receive the menu 2, generated by Home / Portfolio
projects sequence. More details below. */
</nav>
<?php } ?>
<?php if ($header_search != 'off') { do_action( 'thb_quick_search' ); } ?>
<?php if ($header_cart != 'off') { do_action( 'thb_quick_cart' ); } ?>
<a href="#" data-target="open-menu" class="mobile-toggle<?php if (!$full_menu_true) { ?> always<?php } ?>">
<div>
<span></span><span></span><span></span>
</div>
</a>
</div>
</div>
</header>
<!-- End Header -->
At this point, my question is, how can I implement into the header.php the following html sequences that generate the rollover images linked to the menu buttons, keeping in mind that there are different sections, each menu with his section as follow. Home / Portfolio / About:
<nav>
<ul>
...
</ul>
<div id='menuRight'>
<div>
Home
<img src='http://images5.fanpop.com/image/photos/31100000/random-space-space-31155586-598-398.jpg'>
</img>
</div>
<div>
Portfolio
<img src='http://www.keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg'>
</img>
</div>
<div>
About
<img src='http://images5.fanpop.com/image/photos/31100000/random-space-space-31155586-598-398.jpg'>
</img>
</div>
</div>
</nav>
and menu 2, Home / Portfolio projects:
<nav>
<ul>
...
</ul>
<div id='menuRight'>
<div>
Home
<img src='http://images5.fanpop.com/image/photos/31100000/random-space-space-31155586-598-398.jpg'>
</img>
</div>
<div>
Fiva
<img src='http://www.keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg'>
</img>
</div>
<div>
Caterham
<img src='http://images5.fanpop.com/image/photos/31100000/random-space-space-31155586-598-398.jpg'>
</img>
</div>
<div>
Mobile
<img src='http://www.keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg'>
</img>
</div>
</div>
</nav>
I left intentionally the css out of discussion because, that's another chapter of this code tweak.
LE: I have to mention that the rollover image effect activated by the menu buttons, will be enough (and make sense) for it to be available, only on the Home page and Portfolio page of the site. I think that It could be very tricky to achieve the same effect when we have a project page opened (let's say FIVA) and have the mouse over another project button for example.
LE2: regarding the rollover images, I am not looking for a fancy php code that's supposed to grab a preview of the last project or something; a php code that allows me to predefine image source links like we have in the above html menu sections, will be just fine, taking in consideration the fact that these images will not be replaced so often.
LE3: Pure experimental, and please correct me, I was just thinking, using include PHP function to call two separate html files in header.php (including in these the above described menu 1 and 2 sections) could be the beginning of a workaround?
<!-- Start Header -->
...
<div class="myrow max_width ">
...
<ul class="full-menu">
<li>Please assign a menu from Appearance -> Menus</li>
</ul>
<?php } ?>
<?php
/* But there still should be a php code, that call the
html sections *if* we are on Homepage or Portfolio page.
Something like:
Php instructions, if Home page */
include('menu1section.html');
// and also php instructions, if Portfolio page
include('menu2section.html');
?>
</nav>
<?php } ?>
<?php if ($header_search != 'off') { do_action( 'thb_quick_search' ); } ?>
...
</div>
</header>
<!-- End Header -->
Any thoughts? Thank you,

I'm not a wordpress dev, but it seems to me you need a way to detect the active category/post/page? you are on... it's difficult to understand your data structures from looking at the website.
<!-- Start Header -->
...
<div class="myrow max_width ">
...
<ul class="full-menu">
<li><a href="<?php echo get_admin_url().'nav-menus.php'; ?>">
Please assign a menu from Appearance -> Menus
</a></li>
</ul>
<?php } ?>
<?php
$thisCat = get_category(get_query_var('cat'));
switch ($thisCat->name) {
case 'Home':
echo "Home test";
break;
case 'Portfolio':
echo "portfolio test";
break;
case 'About Us':
echo "About Us test";
break;
}
?>
....
<!-- End Header -->
Revised my answer to give you a better idea of what test I'm referring too.

Related

Fix stacked icons in Main Header on WordPress site using php?

Disclaimer:
My knowledge about coding and website development is basic and self-tought. I do not know what i am doing and will likely not understand your answer.
Situation:
I am developing a website (e-commerce) using WordPress (woocommerce) and am using a theme (Shopkeeper) which I am not willing to abandon given the amount of work put into it already. Nevertheless, I am stuck on a major layout problem with the website's main header.
Shopkeeper theme comes with 5 default main header layouts. None of which achieve what I want. I have selected the layout most closest to what I want (Layout 5).
The header consists of 2 parts:
Logo [Part_1]
Tool icons (whishlist, cart, account and search) [Part_2]
As of right now, [Part_1] and [Part_2] are one monolithic group that I cannot move independently. I want to achieve that Layout 5 goal and have them positioned in separate places.
Failed attempt:
My approach was to dig through the Theme files and find the source code for Layout 5 to change it (given that Shopkeeper Theme does not provide the user with the option to create or modify existing Layouts through the UI). I have found the code below which is contained in the header-centered-menu-under.php file:
Original code corresponding to Layout 5
<?php
$header_width = ( 'full' === Shopkeeper_Opt::getOption( 'header_width', 'custom' ) ) ? 'full-header-width' : 'custom-header-width';
$mobile_class = wp_is_mobile() ? 'is-mobile' : '';
?>
<header id="masthead" class="site-header menu-under <?php echo esc_attr($header_width); ?>" role="banner">
<div class="row">
<div class="site-header-wrapper <?php echo esc_attr($mobile_class); ?>">
<div class="site-branding">
<?php shopkeeper_get_logo(); ?>
</div>
<div class="menu-wrapper <?php echo esc_attr($mobile_class); ?>">
<?php if( !wp_is_mobile() ) { ?>
<?php shopkeeper_get_menu( 'show-for-large main-navigation default-navigation', 'main-navigation', 1 ); ?>
<?php } ?>
<div class="site-tools">
<?php echo shopkeeper_get_header_tool_icons(); ?>
</div>
</div>
</div>
</div>
</header>
I have proceeded to change the code as to have two "independent" Parts. This is what I came up with:
Modified code corresponding to Layout 5 modified
<?php
$header_width = ( 'full' === Shopkeeper_Opt::getOption( 'header_width', 'custom' ) ) ? 'full-header-width' : 'custom-header-width';
$mobile_class = wp_is_mobile() ? 'is-mobile' : '';
?>
<header id="masthead" class="site-header menu-under <?php echo esc_attr($header_width); ?>" role="banner">
<div class="site-header-wrapper <?php echo esc_attr($mobile_class); ?>">
<div class="site-branding">
<?php shopkeeper_get_logo(); ?>
</div>
</div>
</header>
<header id="masthead" class="site-header menu-under <?php echo esc_attr($header_width); ?>" role="banner">
<div class="menu-wrapper <?php echo esc_attr($mobile_class); ?>">
<?php if( !wp_is_mobile() ) { ?>
<?php shopkeeper_get_menu( 'show-for-large main-navigation default-navigation', 'main-navigation', 1 ); ?>
<?php } ?>
<div class="site-tools">
<?php echo shopkeeper_get_header_tool_icons(); ?>
<style>
.site-tools {
position: relative;
margin-top: 1%;
margin-left: 70%;
}
</style>
</div>
</div>
</header>
Problem:
While I seem to have achieved separating [Part_1] from [Part_2] and placed [Part_2] more or less where I wanted, I now have 2 new issues with my Layout 5 modified:
the icons are now stacked (as opposed to their horizontal alignment before the change)
the icons are now preceded by a bullet
What have I done wrong and how can I have the icons aligned horizontally without the bullets ?

Highlight current item in the heading php

So I was wondering how do you highlight the current page in the heading of the website. I used the include method to call the heading in all of the pages, but how do I highlight the current page while maintaining the include method in every page.
HEADER.PHP
<div id="site-content">
<header class="site-header">
<div class="container">
<a href="index.php" id="branding">
<img src="images/logo.png" alt="Company Name" class="logo" width="100">
<div class="branding-copy">
<h1 class="site-title" style="font-size:60px;">Benedicto Law</h1>
<small class="site-description">Justice In Words</small>
</div>
</a>
<nav class="main-navigation">
<button type="button" class="menu-toggle"><i class="fa fa-bars"></i></button>
<ul class="menu">
<li class="menu-item">Home</li>
<li class="menu-item">Attorney</li>
<li class="menu-item">Service</li>
<li class="menu-item">Contact</li>
</ul>
</nav>
<nav class="mobile-navigation"></nav>
</div>
</header> <!-- .site-header -->
then I only used
<?php include_once('header.php') ?> for calling it to other pages.
I wanted it to highlight the current menu item where the user is in.
For example:
The user pressed Attorney button the Attorney button in the heading should have a highlight.
Any help is appreciated thanks
You have at least 2 options.
1. Pass the data in PHP manually
Before your include(header statement, create a variable for the onPage and set it. Then use it in your include.
For example:
<?php
$onPage = 'attorney';
include_once('header.php') ?>
Then in header.php, check for it like this:
<li class="menu-item <?php if ($onPage == 'attorney') echo 'active'; ?>">Attorney</li>
2. Automatically detect it
In header.php
$onPage = $_SERVER['PHP_SELF'];
// make sure it's getting just the page, no directory
$onPage= explode("/",$onPage);
$onPage = $onPage[count($onPage-1)];
// remove .php and make sure its lower case
$onPage = str_replace(".php", "", strtolower($onPage));
// for /directory/Attorney.php this will give you 'attorney'
The check for it like before
<li class="menu-item <?php if ($onPage == 'attorney') echo 'active'; ?>">Attorney</li>

Wordpress nested if is_active_sidebar within another is_active_sidebar

I have a theme that displays a menu of products via widgets. We are able to show/hide these categories (sidebars) via the code below. Right now it will show/hide this carousel-item if one of the two product categories have widgets. We would like keep this functionality, but hide the category if it is empty. For example if there are products in category 1, but not in category 2 we want to hide an entire section (see notes in code of where we want this.
<?php if ( is_active_sidebar( 'widget-category-1' ) || is_active_sidebar('widget-category-2') ) { ?>
<div class="carousel-item active animated fadeIn">
<div class="d-block w-100">
//show or hide start here//
<div class="container-fluid px-5">
<div class="row">
<ul class="three-col">
<?php dynamic_sidebar( 'widget-category-1' ); ?>
</ul>
</div>
</div>
//show or hide end here//
//show or hide start here//
<div class="container-fluid">
<div class="row">
<ul class="three-col">
<?php dynamic_sidebar( 'widget-category-2' ); ?>
</ul>
</div>
</div>
//show or hide end here//
</div>
</div>
<?php } ?>`
You can use first-of-type or nth-of-type to reference the element you want to toggle. That or possibly a not() pseudo class should do the trick.
I added a CSS tag to your question. You would need to add the rest of the element that dynamic_sidebar is returning to be sure but typically there is a 'has-items' identifier you can reference.
We ended up wrapping each section in a <div class="phide"></div> and used javascript to hide the this div if no <li></li> is present
<script>
(function($) {
$( document ).ready(function() {
$('.phide:not(:has(li))').hide();
});
})(jQuery);
</script>

Wordpress 3.8.1 TinyMCE does not seem to be rendering my pages' <div> elements

I've created several pages in wp-admin and inserted the needed HTML. Each page contains multiple nested div elements. An example of one of my pages is the following Contact page:
<div id="content">
<div id="inner-content" class="row clearfix">
<div id="main" class="large-12 medium-12 columns clearfix" role="main">
<strong>Artist Name</strong>
<div class="clearfix"></div>
<a class="email" href="mailto:example#gmail.com">Email Artist</a>
<h3 class="team-title">Artist's Team</h3>
<ul class="team-list">
<li>
<strong>Sarah</strong><p class="team-role"> : Fine Art Agent</p>
<p class="company">Some Agency, NYC</p>
<a class="email" href="mailto:example#gmail.com">Email Sarah</a>
</li>
<li>
<strong>Justin</strong><p class="team-role"> : Manager</p>
<div class="clearfix"></div>
<a class="email" href="mailto:example#gmail.com">Email Justin</a>
</li>
<li>
<strong>Jesse</strong><p class="team-role"> : Publicist</p>
<p class="company">Another Art Agency</p>
<a class="email" href="mailto:example#gmail.com">Email Jesse</a>
</li>
</ul>
</div> <!-- end #main -->
</div> <!-- end #inner-content -->
For this Contact page I have chosen the Contact (contact.php) template for it in the Page Attributes > Template dropdown menu like so:
And the contents of the contact.php are:
<?php /* Template Name: Contact */ ?>
<?php get_header(); ?>
<?php get_footer(); ?>
For some reason when I go to view the Contact page, all I see is the header and footer. All of the HTML code get's ignored by Wordpress. Why is this happening? And how to fix it?
well your contact.php is missing the post output, the minimal code should be something like that
<?php the_post(); the_content(); ?>
e.g. set the first post as current (skipping the loop) then output its content. though as you added plain html to the page content (often seen as a no-no) it maybe better to directly echo content like that:
<?php the_post(); global $post; echo $post->post_content; ?>
that way you skip wordpress filters that add paragraphs etc. to the content.

How to create pages in Wordpress that are not visible in menus

I want to create some pages in wordpress, but I dont like these pages visible in menus. How can I create such pages ?
thanks
Use something like Exclude Pages « WordPress Plugins or work with Function Reference/wp list pages « WordPress Codex to exclude a page
If you're using/creating a custom theme or don't mind extending the one you're using, you could explicitly specify which page names or ids you want to be in the menu by editing the links. In my custom theme I just went in and destroyed the dynamically-generated links, replacing them with my own.
I chose to do this in one of my own projects because I wanted to be able to have a lot of non-navigation pages without having to keep adding to exclude_pages.
EDIT (to be more specific):
Navigation in the default theme (wp-content/themes/twentyten) is in the header.php file and looks like this:
<div id="access" role="navigation">
<?php /* Allow screen readers / text browsers to skip the navigation menu and get right to the good stuff */ ?>
<div class="skip-link screen-reader-text"><?php _e( 'Skip to content', 'twentyten' ); ?></div>
<?php /* Our navigation menu. If one isn't filled out, wp_nav_menu falls back to wp_page_menu. The menu assiged to the primary position is the one used. If none is assigned, the menu with the lowest ID is used. */ ?>
<?php wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary' ) ); ?>
</div><!-- #access -->
If you view-source on the page this generates in a default WordPress install, it becomes the following html:
<div id="access" role="navigation">
<div class="skip-link screen-reader-text">Skip to content</div>
<div class="menu">
<ul>
<li class="current_page_item">Home</li>
<li class="page_item page-item-2">About</li>
</ul>
</div>
</div><!-- #access -->
So, as you can see, if you wanted to customize the navigation you would just remove that wp_nav_menu line and replace it with the appropriate html. Say you want your navigation to go to the Home, Cool Stuff and About Us pages. That could accomplished with the following code in header.php:
<div id="access" role="navigation">
<?php /* Allow screen readers / text browsers to skip the navigation menu and get right to the good stuff */ ?>
<div class="skip-link screen-reader-text"><?php _e( 'Skip to content', 'twentyten' ); ?></div>
<?php /* Our CUSTOM navigation menu. */ ?>
<div class="menu">
<ul>
<li class="<?php if (!is_paged() && is_home()) { ?>current_page_item<?php } else { ?>page_item<?php } ?>">Home</li>
<li class="<?php if (is_page('cool-stuff')) { ?>current_page_item<?php } else { ?>page_item<?php } ?>">Cool Stuff</li>
<li class="<?php if (is_page('about-us')) { ?>current_page_item<?php } else { ?>page_item<?php } ?>">About Us</li>
</ul>
</div>
</div><!-- #access -->
This is usually dependent on your theme I think...
If I'm not wrong, some themes will have an option on the "Theme Options" page. If the option isn't there, the author of the theme you're using hasn't provided it, so you'll need to either recode the theme or switch to a different one.

Categories