I want to make a menu that will dynamically show the active static pages from CMS; for example if in my CMS I have these pages:
About Us (enabled)
Shipping & Refund (disabled)
Terms and Conditions (enabled)
Contacts (enabled)
then the menu would look like:
About US | Terms and Conditions | Contacts
I need just a few tips on how to get started; maybe somebody has already done this before?
Dougle
thanks a lot, that was really helpful!
Fede
in Magento CMS you can make static pages that you can only access using its IDENTIFIER; what I wanted is somehow make a menu that will automatically display the ACTIVE (enabled) static pages; and if you set status to Disable it should not be in the menu;
here is the code i used, note there is IF $PageData['identifier']!='no-route'; no-rute is the 404 page, so i don't need it in the menu, but it must be enabled so Magento redirects 404 errors to this page;
<div>
<?php $collection = Mage::getModel('cms/page')->getCollection()->addStoreFilter(Mage::app()->getStore()->getId());?>
<?php $collection->getSelect()
->where('is_active = 1'); ?>
<ul>
<?php foreach ($collection as $page): ?>
<?php $PageData = $page->getData(); ?>
<?php if($PageData['identifier']!='no-route') { ?>
<li>
<?php echo $PageData['title'] ?>
</li>
<?php } ?>
<?php endforeach; ?>
</div>
To exclude more than just the no-route I added a new field to the CMS pages to specify if the page should have a menu item or not using true or false. I followed Add a new CMS Field and used the following in main.php
$fieldset->addField('menu', 'text', array(
'name' => 'menu',
'label' => Mage::helper('cms')->__('On Menu'),
'title' => Mage::helper('cms')->__('On Menu'),
'required' => true,
'disabled' => $isElementDisabled
));
Then changed this line:
<?php if($PageData['identifier']!='no-route') { ?>
to
<?php if($PageData['menu']!= 'false') { ?>
Here is another way to put static links to Magento catalog menu.
First, create static page, assign some url key to it, for example, "my-test-page".
Go to /app/code/core/Mage/Catalog/Block, copy file Navigation.php to /app/code/local/Mage/Catalog/Block, now you able to edit it without any worries about the possibility of loosing your changes with Magento upgrade.
Open file Navigation.php at line 265 (magento 1.4) function _renderCategoryMenuItemHtml(...), change code:
$htmlLi .= '>';
$html[] = $htmlLi;
$html[] = '<a href="'.$this->getCategoryUrl($category).'"'.$linkClass.'>';
$html[] = '<span>' . $this->escapeHtml($category->getName()) . '</span>';
$html[] = '</a>';
to that:
$htmlLi .= '>';
$html[] = $htmlLi;
if(preg_match('/\/static-/', $this->getCategoryUrl($category))) {
$link_url = str_replace("static-", "", $this->getCategoryUrl($category));
} else {
$link_url = $this->getCategoryUrl($category);
}
$html[] = '<a href="'.$link_url.'"'.$linkClass.'>';
$html[] = '<span>' . $this->escapeHtml($category->getName()) . '</span>';
$html[] = '</a>';
Now go to Categories management, edit category, change URL key to that: "static-my-test-page" and uncheck "Create Permanent Redirect for old URL" check-box. After saving category you will have link to my-test-page at top categories menu in Magento.
So after all that changes you can convert category link to static page link by adding prefix "static-" to category URL key.
In your page/html block create a method containing:
$collection = Mage::getModel('cms/page')->getCollection()->addStoreFilter(Mage::app()->getStore()->getId());
$collection->getSelect()
->where('is_active = 1')
->order('main_table.sort_order ASC');
return $collection;
Which you can call in your template and foreach() through creating your LIs
Might need some tweaking mind, depending on your setup.
From memory though i think this is built in, have a look in design/frontend/../../templates/page/ i seem to remember striping out some similar functionality in one of the phtml files in there.
where, order and other select stuff can be found in /lib/Zend/Db/Select.php(FYI)
Related
I'm having an issue where, even though the page renders originally with "All" selected in the dropdown, if you go to a category (renders fine) and then back to "All" in the dropdown, you see a single post. This is instead of the originally displayed page with all of the categories. I need the page to basically render the same page for "All" regardless. Any thoughts?
wp_dropdown_categories('show_option_all=All&hide_empty=0&show_count=0&orderby=name&echo=0');
I did a similar post on WPSE last week and it seems that the two might be related. For convenience, here is the post
Here is a variation of the code that you use. I'm using get_categories() here to achieve the same goal. I had to adjust my code slightly to make it acceptable for your need.
There are a however other modifications you have to make for this to work. When you select the All Categories option, you will be taken to a page that will display what ever you need to display. This page you will have to manually create
There is no index archive pages in Wordpress as you might know. (Check out this post I have done on the same subject). What this means is, domain.com/category/ returns a 404.
So, to make this all work, you'll have to make a copy of page.php, rename it to something like page-category.php (see the codex on how to create custom page templates), open it up, create your custom query to display what you would like to display when this page is visited
You now need to create your page in the back end. I would suggest that you use the slug category so that when you visit domain.com/category/, this page would be displayed. (Just remember, you cannot create child pages for this page, it will break the hierarchy). I have also made the code to go to domain.com/category/ when All Categories is selected
Apart from that, the code should work fine. You just need to check the URL structures maybe, and also set the parameters in get_categories() to suite your needs. Here is the drop down code.
<select name="event-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'>
<option value=""><?php echo esc_attr(__('Select Category')); ?></option>
<?php
$option = '<option value="' . get_option('home') . '/category/">All Categories</option>'; // change category to your custom page slug
$categories = get_categories();
foreach ($categories as $category) {
$option .= '<option value="'.get_option('home').'/category/'.$category->slug.'">';
$option .= $category->cat_name;
$option .= ' ('.$category->category_count.')';
$option .= '</option>';
}
echo $option;
?>
</select>
EDIT
I actually had an idea here that will come in handy. I've recently done an answer on displaying all categories in a list with all post titles under the specific category. This same idea can be used in your page-category.php template.
When a user selects the All Categories option, they will be taken to this page which will list all categories and post title.
Here is the complete code: (for an explanation of the code, see my post here)
In your functions.php
add_action( 'transition_post_status', 'publish_new_post', 10, 3 );
function publish_new_post() {
delete_transient( 'category_list' );
}
In your template where you need to display your list
<?php
if ( false === ( $q = get_transient( 'category_list' ) ) ) {
$args = array(
'posts_per_page' => -1
);
$query = new WP_Query($args);
$q = array();
while ( $query->have_posts() ) {
$query->the_post();
$a = '' . get_the_title() .'';
$categories = get_the_category();
foreach ( $categories as $key=>$category ) {
$b = '' . $category->name . '';
}
$q[$b][] = $a; // Create an array with the category names and post titles
}
/* Restore original Post Data */
wp_reset_postdata();
set_transient( 'category_list', $q, 12 * HOUR_IN_SECONDS );
}
foreach ($q as $key=>$values) {
echo $key;
echo '<ul>';
foreach ($values as $value){
echo '<li>' . $value . '</li>';
}
echo '</ul>';
}
?>
I am using the customizr theme on wordpress and I want to edit my footer menu. On the help page I found this php code. I put it in functions.php and it does work but I want to add another menu.
add_filter('tc_credits_display', 'my_custom_credits');
function my_custom_credits(){
$credits = '';
$newline_credits = '';
return '
<div class="span4 credits">
<p> · © '.esc_attr( date( 'Y' ) ).' '.esc_attr(get_bloginfo()).' · '.($credits ? $credits : 'Designed by Themes & Co').' ·'.($newline_credits ? '<br />· '.$newline_credits.' ·' : '').'</p> </div>';
}
The 1st one is copyright, 2nd one is themes & co link, I want to add another one for terms of service.
'Terms of Service').'
How do I do it?
to add another menu you need to do two things first declare the menu in the themes function file like so:
function register_my_menu() {
register_nav_menu('header-menu',__( 'Header Menu' ));
}
add_action( 'init', 'register_my_menu' );
Where register_my_menu can be whatever you want to call the function, header-menu needs to be unique and match the following command.
Then:
<?php wp_nav_menu( array( 'theme_location' => 'header-menu' ) ); ?>
to echo out the menu in the theme files...
please read this: http://codex.wordpress.org/Navigation_Menus
Hi im a web dev an im trying to get word press to use a 2nd navigation for my blog section. i have the main site all set up but what i want is when i hit the blog in my top nav to show a differnt navigation for the blog section. i have tried using the following code in the header.php
<?php
if (is_single('blog')){
wp_nav_menu(array('menu'=>'secondary' ));
}
?>
but this code does not seem to work even though the theme supports 2 navigations.
<nav class="Seo-nav">
<div class="Seo-nav-inner">
<?php
echo theme_get_menu(array(
'source' => theme_get_option('theme_menu_source'),
'depth' => theme_get_option('theme_menu_depth'),
'menu' => 'primary-menu',
'class' => 'Seo-hmenu'
)
);
get_sidebar('nav');
?>
the above code is the code i use to call my navigation. is there any way to get a specific page or pages to show the one menu? any help on this would be great. never had to make certain pages have different navigation before so this is a new one on me.
Like was said in comments earlier, you'll need Conditional Tags. Probably for you specifically, the is_page() conditional.
<?php
if (is_page('blog')) {
// Echo this menu on the blog slug page
wp_nav_menu(array(
'theme_location' => '[name_of_menu]'
));
} else {
// Otherwise, echo this one
wp_nav_menu(array(
'theme_location' => '[name_of_main_menu]'
));
}
See the codex for the wp_nav_menu attributes. Can print out the menu other ways, that's just the one I recommend.
Depending how you blog is set up, is_page('blog') may need to be is_home(), 'blog' == get_post_type(), or some other variation.
I think you can go here two ways.
One is to only show children of current page on the second like
function show_different_nav(){
global $post;
$sec_nav=wp_list_pages("child_of=".$post->ID);
return $sec_nav;
}
now call show_different_nav() in your template.
The other way would be to write an own filter. I modified the http://wordpress.org/plugins/get-different-menus/ in this case.
add_filter( 'wp_nav_menu_items', 'get_different_custom_menu_item_according_to_page_slug', 10, 2 );
function get_different_custom_menu_item_according_to_page_slug ( $items, $args ) {
$current_id=(int)$GLOBALS['post']->ID;
$current_slug=$GLOBALS['post']->slug;
$the_nmo = wp_get_nav_menu_object($current_slug);
if ($the_nmo==""){}else{
$menu_id=$the_nmo->term_id;
$items_2 = wp_get_nav_menu_items($menu_id);
$items="";
if ($args->theme_location == 'secondary') {
foreach ($items_2 as $item){
$link_page_id = (int)($item->object_id);
if ($current_id == $link_page_id){$cur=" current-menu-item current_page_item";}else{$cur="";
}
$items .= '<li class="menu-item menu-item-type-post_type menu-item-object-page'.$cur.'">'.$item->title.'</li>';
}
}
}
return $items;
}
In my opinion you need a menu for this called exactly the same as the page slug.
I am not sure if this is possible !
I have two files say main.php and submenu.php
In main.php i have the following mainMenu
and in another file called submenu.php i have a list say
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
If i click on Pricelist in main menu i want to display the content of the submenu as a list under PriceList as
The simple solution may be including the list view in menu.php itself, But that's not the situation
Thank you.
You should use arrays and recursion. Like so:
(code is rough, sorry)
$menu = array(
'About Us',
'Categories',
'Price List' => array(
'1','2','3','4'
)
);
function loop_menu($menu){
foreach($menu as $m):
if(is_array($m))
return '<ul>' . loop_menu($m) . '</ul>';
else
return '<li>' . $m . '</li>';
endforeach;
}
echo loop_menu($menu);
Then use CSS to make it look however your want.
If you want to include a page just use
include('page.php');
But using JS & CSS is the best way
Here is a very helpful plugin
http://users.tpg.com.au/j_birch/plugins/superfish/
And don't forget to get jQuery
I want to add a sub menu item "Locations" to "Catalog" menu item in opencart administration. On selecting locations, I want to see my own location management view page which inetracts with my own locations table in the opencart database.
Please let me know where and what mvc's to create to achieve this functionality in open cart.
Thank you.
How to create a opencart admin module??
You can simply do this by adjusting:
Admin > controller > view > template > common > header.tpl
You can simply make adjustments to the menu on this page (static changes). To actually create modules for you and your staff etc. Then follow the tutorial of MVC posted on this page:
How to create a custom Admin Page in Opencart?
I have already implemented your concept in my opencart project.
Notes:
1) by default in product adding dashboard page have a field to enter product location you fill product location there and follow my points
2) open catalog > model > category.php add this code
function getCategoryLoction($category_id) {
$sql = "select p.location,count(p.location) as locCount from " . DB_PREFIX . "product p inner join " . DB_PREFIX . "product_to_category p2c on(p.product_id=p2c.product_id) where p2c.category_id=$category_id group by p.location";
$query = $this->db->query($sql);
return $query->rows;
}
3) open catalog > controller>module > category.php add this code
/* location based search starts here */
$incomingCatId = ($this->data['category_id']!= '')?$this->data['category_id']:'0';
$locations = $this->model_catalog_category->getCategoryLoction($incomingCatId);
foreach($locations as $loc):
$this->data['locations'][] = array(
'location' => $loc['location'],
'count' => $loc['locCount'],
'href' => $this->url->link('product/category', 'path=' . $incomingCatId.'&loc='.$loc['location'].'')
);
endforeach;
/* location based search ends here */
4) open catalog > view >theme >default >template>module > category.tpl category add this code
<div class="l_nav_box">
<div class="l_nav_title">
<h6>Location</h6>
</div>
<ul class="cat_ul">
<?php if(!empty($locations)): ?>
<?php foreach ($locations as $loc) : ?>
<?php if($loc['location']!= ''): ?>
<li> <?php echo $loc['location']; ?> <span>(<?php echo $loc['count']; ?>)</span> </li>
<?php endif; ?>
<?php endforeach; ?>
<?php else: ?>
No Locations mentioned
<?php endif; ?>
</ul>
</div>
5) important in admin side activate category module and save it choose