How to add external link to top menu item in magento - php

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

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.

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 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"; ?>

Magento 1.7.2 - How to include CMS pages in topmenu?

I have created 2 CMS pages in magento 1.7.2
Lets say the cms page "About Us" which is the URL
http://localhost/magento/index.php/about-company/?___store=default
and the page "Customer Service" which is the URL http://localhost/magento/index.php/customer-service/?___store=default
In my HEADER.PHTML the line getChildHtml('topMenu') ?> shows the topmenu.
the problem is that the 'topMenu' contains only the categories created by Catalog->Manage Categories
What is the appropriate way to include the 2 cms pages ("About Us" and "Customer Service")
in the 'topMenu' ?
Thank you for your help !
Create a static block for cms pages from admin and write the below format code
<ul>
<li>About Company</li>
<li>Customer Service</li>
</ul>
Call this block in topmenu.phtml (/template/page/html/topmenu.phtml) page
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('cms_pages')->toHtml();?>
Add following code under your /template/page/html/topmenu.phtml
<li title="<?php echo $this->__('About Company') ?>"><?php echo $this->__('About Company') ?></li>

Magento Redirect URL?

Hellow friemds i develop a magento website and i created page and i want to linked that page into navigation menu bar
http://www.magentocommerce.com/knowledge-base/entry/adding-page-links-in-the-navigation-bar
Starting With Magento CE 1.7 there is an event that lets you add anything to the top menu.
The event is page_block_html_topmenu_gethtml_before. In the default theme, this is used to add categories in the menu. Check out the method Mage_Catalog_Model_Observer::addCatalogToTopmenuItems(). You can do the same.
If you are working on versions before 1.7 just edit the file app/design/frontend/{interface}/{theme}/template/catalog/navigation/top.phtml and add your link inside the ul element:
<ul id="nav">
<?php echo $_menu ?>
<li><?php echo $this->__('Text here')?></li>
</ul>
or like this
<ul id="nav">
<?php echo $_menu ?>
<li><?php echo $this->__('Text here')?></li>
</ul>
The approach for version 1.6 works in 1.7 also but it's not that clean.

Categories