Make Menu Template Dynamic - php

I'm currently developing a website and decided to have the menus to be written in a separate file.
Currently I have a php page holding the menu list called menu.php
<?php
$menu='<div class="headMenu">
<ul>
<li><span>Home</span></li>
<li><span>Transaction</span></li>
<li><span>Schedule</span></li>
<li><span>Book</span></li>
</ul>
</div>';
echo $menu;
?>
Whenever you enter the Transaction Page, the distinct code for that page is
<li class="active"><span>Transaction</span></li>
and same as the other pages,
I'm thinking this can be done in jquery using its .addClass() method, but I have no idea of passing variables from one page to the separate page holding the menu written in php

Why not to do a function in menu.php that returns a string containing menu with "activated" page?
If I understand your question correctly, this would be helpfull:
<?php
function renderMenu($site){
$menu='<div class="headMenu">
<ul>
<li '. ($site=="index.php"?'class="active"':'') .'><span>Home</span></li>
//etc...
</ul>
</div>';
return $menu;
?>
And then in your main script:
echo renderMenu(__FILE__);

Related

highlight a wordpress nav link for a single page

I have an archive events page that links to a single page that lists all previous events created in the past and can only be visited via the archive events page.
My question is, how can I highlight the archive events page url link when viewing this single page?
For example the below snippet targets a hard-coded nav link to highlight the events link when viewing the other page, however my menu is not hard-coded:
<nav class="main-navigation">
<ul>
<li>...</li>
<li>...</li>
<li>
<?php if(get_post_type() == 'event' OR is_page('past-events')) echo 'class="current-menu-item"';?>>
events
</li>
<li>...</li>
</ul>
</nav>
The tutorial I am following does not give the alternative coded way of targeting this similarly in the functions.php page, for a dynamic menu that is created via the wordpress dashboard menu.
I have tried searching for examples, but I havent found anything that has helped so far and in need of some outside help.
How can I do this?
I can see something wrong. you haven't put the php code in right place. corrected code is this. try this and let me know if it works
<nav class="main-navigation">
<ul>
<li>...</li>
<li>...</li>
<li <?php if(get_post_type() == 'event') echo 'class="current-menu-item"';?>>
</li>
<li>...</li>
</ul>
</nav>
Ok, I think I have found a solution -- being a novice -- has worked!
Hopefully someone can point out any changes/improvements I could make, but here it is:
To highlight a menu link for a different page than the one that you are on/viewing, for example in my scenario: I have an archive events page and within that page I have a link that takes you to a single page that lists all past previous events, but that has no link in the main navigation to it.
function nav_class($classes) {
// Remove "current-menu-item" class
$classes = array_diff($classes, array('current-menu-item'));
// If this is the "past-events" page, highlight the event menu item
if (in_array('menu-item-36', $classes) && is_page('past-events')) {
$classes[] = "current-menu-item";
}
return $classes;
}
add_filter('nav_menu_css_class', 'nav_class', 10, 2);
I hope this helps someone else out.

PHP dynamic navigation submenu

I have created a dynamic nav menu for a website. The menu loads to each page from a separate file, therefore i am using php required on all pages. I am trying to highlight the menu item when visitor is on each page and i cannot use JS or jQuery. All should be done in PHP, CSS and HTML only.
So far, i have the following function in the navigation file:
<?php
function setActive($name){
global $pageName;
if ($pageName == $name){
echo "class='active' ";
}
}
?>
And example of a menu:
<ul>
<li ><a <?php setActive ('v')?> href="v.php">Home</a></li>
<li>
cd
<ul class="hidden">
<li>a</li>
<li>b</li>
</ul>
</li>
</ul>
The function works for main menu, but i cannot get it work for submenu pages. I want the item cd to highlight when visitor lands on page a.php or b.php.
Any ideas how to solve it?
Thanks

Conditional Wordpress Header

I'm trying to create a header to be used in my WP posts so that it displays a list item and and anchor according to the post's author. For example, if, and only if, the author has ID of 6, the header will have one more list item with an anchor to, let's say, page A. If it's not that author I don't want it to display the list item.
So in my single-post.php template I load this header I want with:
<?php get_header('bananas'); ?>
and in header-bananas.php I have:
<nav>
<ul>
<li>Home</li>
<li>Clinic's Blog</li>
<?php if (!is_author(6)){ ?>
<li id="apples">Link to page A</li>
<?php } ?>
</ul>
</nav>
However I suspect Wordpress is not looping in the header so it can't detect if the conditional is true before loading the post in single-post.php, because #apples is displaying even if that post isn't written by author of id 6.
Am I right? If so, what's the best approach to load a conditional with a Wordpress function in the header? Thank you very much!
I was using the wrong function.
The correct way of doing this would be something like this:
<?php
$myauthor = get_the_author_ID($post->ID);
if ($myauthor == 6){
echo '<li id="apples">Link to Page A</li>';
};
?>

How to add external link to top menu item in magento

I am using Magento 1.9.
My top menu contains categories as menu items.
I have managed to open static CMS page when clicked on menu item (i.e. category) by adding custom URL rewrite rule from admin panel.
How I can add menu item with external link to it so that page will get redirected to other website when clicked on menu item.
My Magento website is going to be under subdoamin and external link will be of main doamin.
Magento site will be hosted on http://domain.xyz.dom/ and I need to redirect to http://www.xyz.dom/abc when clicked on menu item.
The way you added cms page in "URL Rewrite Management" you can add external url too.
Create a category, in my case category ID is 3
Add a new URL Rewrite from URL rewrite management
Enter ID Path: category/3
Enter Request Path: catalog/category/view/id/3
Enter your external url in "Target Path"
There can be two ways of redirecting the way you are trying.
From Cateagory Url rewrite management which i guess you have tried so far.
Another way is little messy but it will work you just have to add a static link on the template file.
I mean go to the file
app/design/frontend/your_package/your_theme/template/page/html/topmenu.phtml
you will see some code like this
<?php $_menu = $this->getHtml('level-top') ?>
<?php if($_menu): ?>
<nav id="nav">
<ol class="nav-primary">
<?php echo $_menu ?>
</ol>
</nav>
<?php endif ?>
I have replaced this to add a home page link on the menu see below.
<?php $_menu = $this->getHtml('level-top') ?>
<?php $baseUrl = Mage::getBaseUrl();?>
<?php $currentUrl = Mage::helper('core/url')->getCurrentUrl();?>
<?php if($_menu): ?>
<nav id="nav">
<ol class="nav-primary">
<li class="level0 nav-1 <?php if($baseUrl==$currentUrl){echo 'active';}?>">Home</li>
<?php echo $_menu ?>
</ol>
</nav>
<?php endif ?>
In this way you can add your link.
Hope this helps. Happy Coding!!

how to link a page url to wordpress

I'm new to WordPress, I just converted my HTML to WP Theme, but now I'm facing a problem, I couldn't get to work the linking of pages.
The structure of my theme:
index.php
header.php
sidebar.php
footer.php
services.php <- other page in the site
Here's the sample code from index.php:
<div id="menu">
<ul>
<li><a class="current" href="#">Home</a></li>
<li>Services</li>
<li>Free Quote</li>
<li>Customers</li>
<li>About Us</li>
<li>Contact us</li>
</ul>
</div>
But whenever I try to go to services, it's giving internal server error. I don't know why it's happening.
There are two ways in wordpress for linking Pages
For Content Based pages only:
You need to create a page from wp-admin(Admin panel) and can specify the slug of the page
E.g:
<li>Services</li>
^This is called Slug
This page will always run from page.php.
For HTML Changes Pages:
You need to create a page from wp-admin(Admin panel) and create a file containing
page-slug.php, here slug will change according to the name of page.E.g:
Services page will become page-services.php, in this you can insert your own html.
This will run from page-services rather than page.php
Choose as per your requirement.
Try using:
<a href="<?php echo bloginfo("template_url")?>/services.php">
I think in this case you are just not echoing out the value of that function. Also, however you might want this instead:
<a href="<?php echo home_url() ?>/services">
The template_url goes to your theme's folder, not to your website's homepage. I assume the latter is what you're really going for.
try <php echo site_url()."/sevices.php"; ?>

Categories