Wordpress Sub Menu / Show Sister Pages - php

I am currently using this snippet to show children of the current page.
<?php wp_list_pages('title_li=&child_of='.$post->ID.''); ?>
when I click one of those children pages the sub nav disappears because there are no child pages of the current page, so what I need it to do is continue to show the sister pages of the current page.
What is the best way to do this?

You could request the parent ID instead of the post ID if there is no childs on the current page :
$page_query = new WP_Query();
$all_pages = $page_query->query(array('post_type' => 'page'));
$childs = get_page_children($post-ID, $all_pages);
if(!empty($childs)) {
// display the page childs
wp_list_pages($post->ID);
} else {
// display the page siblings
wp_list_pages(wp_get_post_parent_id($post->ID));
}
get_page_children is used to check if there is any child for the current page - the WP_Query part is needed for get_page_children which required an array of all the pages to look in.

Related

How to display all products in a single page in opencart?

I want to display all products in onepage while clicking a single button in main menu and also need to display category in main menu...
thank in advance...!
Display Alll Product in one Page You have to create separate page for displaying all products.
Create Model file catalog\model\catalog\allproduct.php and paste
code this http://pastebin.com/suF5TP3z
Create Controller file catalog\controller\productallproduct.php and
paste below code http://pastebin.com/jZq3hZyc
Create View file
catalog\view\theme\default\template\productallproduct.tpl and paste
below code http://pastebin.com/1HNh3x73
4 . Create Language file catalog\language\en-gb\product\allproduct.php
and paste below code http://pastebin.com/EcyJH7F9
Enable module from back-end You can see link in menu
Display All Category in Menu
Open and edit your category:
Select tab Data:
Find and Check:Top: Display in the top menu bar.
Note : Only works for the top parent categories.You will need to check all the top level categories.It's Opencart bugs.
I suggest don't make any change in the default theme codes and structure. If you already using a custom theme you can easily create a menu and define to all categories included all products under the one menu or button.
Lets get over with in that way :
For example you have 5 categories. Every category includes over ten products or much.
Create a brand (Manifacturer). This brand will be contain just 1 category. Give it a name like "All Products"
Create a category and name it as "All Products".
Create a menu which will display all products and define the menu display as category. Select the "All Products" category which is you just created.
Go to product pages and click edit for each one. Click the "link" tab and the category textbox, choose "All Products" category. Make this simple edit for every product. I tried for you and it takes 4 / 5 seconds.
Go to "Theme Setting" in your theme. Find "Display Products" or "Products in Category" setting. There are always pre-defined display limit in every theme and it goes next with pagination. Make a simple calculation like that ; how many product in your store, 50 ? 100 ?
Setup the display limit bigger than your total product count.
That's it !
Maybe you would like to play menu css a bit. Like i said before, if you already using a custom theme, you dont need to fork your core codes especially the default theme.
Let me know if any help.
The way i did its something like this
Controller : ControllerProductCategory
inside the index function replace
if (isset($this->request->get['limit'])) {
$limit = $this->request->get['limit'];
} else {
$limit = $this->config->get('config_product_limit');
}
with
if (isset($this->request->get['viewall'])) {
$limit = "";
} else if (isset($this->request->get['limit'])) {
$limit = $this->config->get('config_product_limit');
} else {
$limit = $this->config->get('config_product_limit');
}
Also make this change to display all the products without category filter
replace this code
if ($category_info) {
$this->document->setTitle($category_info['meta_title']);
$this->document->setDescription($category_info['meta_description']);
$this->document->setKeywords($category_info['meta_keyword']);
With this code
if (($category_info) || ($category_id == 0)) {
if ($category_id == 0) {
$this->document->setTitle('all products');
$this->document->setDescription('all products');
$this->document->setKeywords('all products');
$this->data['heading_title'] = 'all products';
$category_info['description'] = '';
$category_info['image'] = '';
} else {
$this->document->setTitle($category_info['name']);
$this->document->setDescription($category_info['meta_description']);
$this->document->setKeywords($category_info['meta_keyword']);
$this->data['heading_title'] = $category_info['name'];
}
and at the last create a new category , you can name it to anything you want. and give the link something like this
https://www.yourwebsite.com/yourCategoryname?viewall=viewall?viewall=viewall
You can place this link anywhere you want and it will load all the products without any pagination.
let me know if you still don't understand anything anyehere.

Checking whether post_parent = 0 in WordPress

I am creating a site using WordPress using a theme which has this PHP at the top of each page and I am confused as to what it does.
<?php
global $post;
global $wp_query;
if ( $post->post_parent != 0 ) {
$thePostID = $post->post_parent;
} else {
$thePostID = $wp_query->post->ID;
};
?>
I was just wondering if anyone could explain exactly what this does? I think it checks to see if the post_parent id is 0 which isn’t allowed in WordPress and sets the post id to the post_parent but I’m not 100% sure.
I think it checks to see if the post_parent id is 0 which isn’t allowed in WordPress
The $post->post_parent is allowed to be 0. If the value is 0, it simply means that the page is a top level page.
A page that has a $post->post_parent other than 0, is a child of another page.
For example, take this page structure as an example:
id page_title post_parent
1 Home 0
2 About 0
3 Staff 2
4 History 2
5 Contact 0
The resulting page/menu structure would be:
Home
About
Staff
History
Contact
The code in question:
if ($post->post_parent != 0) {
$thePostID = $post->post_parent;
} else {
$thePostID = $wp_query->post->ID;
}
I'm not sure why your theme might have the code, but a possible reason might be to get a menu related to the current page. If you're viewing the top level page (i.e. $post->post_parent == 0), then it would show all child pages, or if you're viewing a sub page, the menu might show all sibling pages.
A sample menu generated using this method
Add this to your functions.php file so it's accessible throughout the theme.
/**
* Get top parent for the current page
*
* If the page is the highest level page, it will return its own ID, or
* if the page has parent(s) it will get the highest level page ID.
*
* #return integer
*/
function get_top_parent_page_id() {
global $post;
$ancestors = $post->ancestors;
// Check if the page is a child page (any level)
if ($ancestors) {
// Get the ID of top-level page from the tree
return end($ancestors);
} else {
// The page is the top level, so use its own ID
return $post->ID;
}
}
Add this code to your theme where you want to display a menu. You will need to customise it to suit your particular needs, but it gives you an example of why someone might use the code you asked about.
// Get the highest level page ID
$top_page_id = get_top_parent_page_id();
// Display basic menu for child or sibling pages
$args = array(
'depth' => 1,
'title_li' => FALSE,
'sort_column' => 'menu_order, post_title',
'child_of' => $top_page_id
);
echo wp_list_pages($args);
So as far as i see, this snippet does the following:
Loads the global post object.
Loads the global query object.
Checks (or tries to check) if the current post has a parent filter (?).
If it has, it sets the variable $thePostID to the id of the post_parent parameter of the current post object.
If it hasnt, it sets the post id in the wp_query to the id of the current post.
All together, this seems very unnecessary, bad performing and strange Oo.
Does ist work actually? ^^
I would advise you on using another theme if possible ;-)

If page has children show sub nav

I'm creating a wordpress theme and I'm looking to show the sub nav only if the current page has children, I'm using the code below which works great, but doesn't have an if statement, which means it shows an empty menu on some pages.
<?php global $id;
wp_list_pages("title_li=&child_of=$id&show_date=modified&date_format=$date_format");
?>
Try below :-
$kids = get_pages("child_of=$id");
if (count($kids) > 0) { wp_list_pages(...); }

Drupal template, verify that the page is a menu item

Helló!
I want to examine that the actual page is in the menu.
I want to do with drupal template (page.tpl.php).
For example:
if ($page_is_a_menuitem):
echo "This page is in the menu";
else:
echo "This page not in the menu";
endif;
Because if the page is in the menu a want to highlight the title.
Sorry for my bad english.
Try this
this is return to a all menu list :- menu_get_menus(true)
like this
Array
(
[menu-footer-menu] => Footer Menu
[main-menu] => Main menu
[management] => Management
[navigation] => Navigation
[user-menu] => User menu
)
and you want to get main menu inside a all list to use this
$tree = menu_tree_all_data('main-menu')
this is return in main-menu in all items
otherwise you use a this code
$path = current_path();
$selected_menu= '';
$menu = menu_link_get_preferred($path = NULL, $selected_menu = NULL);
$menu is return a current page manu detail after you want
echo "this menu is ".$menu['menu_name'] .' and menu title is '.$menu['title'];
get the whole menu tree
$tree = menu_tree_page_data('primary-links');
and check in page tpl if the node id exists in the menu...

HighLighted item menu when clicked in codeIgniter

how can i make the active menu in codeIgniter, its not when hover, but when clicked, for example if we are in some category, that item in the menu is highlighted, how can be this done in CI?
Depends on your routing and menu generation script. Esiest method is to check for segments in uri. For example for static menu You can do this:
<?php $class = $this->uri->segment(1)=="someController"?"highlighted":""; ?>
Menu item
There are a few steps involved here.
Firstly, you need to determine which is the 'current' menu item or category. If you can structure your site so that there's a direct relationship between your URL structure and your top level menu items (and / or your categories) then this will help a lot.
You'll need a common section of code that generates your main menu. This code could iterate through an array of menu item titles to produce the HTML for the menu. If the array had keys for the URL segment and values for the menu item text...
$menuItems = Array(
"/home" => "Home",
"/products" => "Products",
"/faq" => "FAQ",
"/aboutus" => "About Us"
);
(Leading slashes included for clarity as to which are the URI segments and which are the Menu Titles only - you would usually omit the leading slash)
... then, while you're iterating through, you could check each item against the relevant segment of the current URL.
Secondly, having worked out which is the current item, you could add a css class to the relevant HTML element.
e.g.
$menuHtml = "<ul class='menu'>\r\n";
foreach($menuItems as $segment => $title) {
if($segment == $this->uri->segment(1)) {
$menuHTML .= "<li class='current'>$title</li>";
} else {
$menuHTML .= "<li>$title</li>\r\n";
}
}
$menuHtml .= "</ul>";
You'd then need to apply the required highlight styles in CSS to the li.current element:
li.current {
<your-highlight-css-styles-here>
}

Categories