I have taken over a wordpress site built by someone else (londonschoolofsamba.co.uk) and am having difficulties with sub-sub-lists disappearing in the navigation.
If I visit a page that has children, then the navigation displays the parent pages, and the children and grandchildren of that page. If I visit one of the pages that is a child, then the navigation still displays the parent and grandchildren, but if I go to a grandchild page, then the navigation returns to displaying only the parents.
Can anyone help me to get it so that when visiting a grandchild page, that the navigation still shows the children and grandchildren, and not just the parents?
<?php
$this_post = $wp_query->get_queried_object();
$temp = get_post_ancestors($this_post);
if(sizeof($temp) > 0)
{
$this_page_id = $temp[0];
}
else
{
$this_page_id = $this_post->ID;
}
$home_page = get_page_by_title('Home');
$pages = get_pages("sort_column=menu_order&parent=0&exclude=".$home_page->ID);
foreach($pages as $page)
{
$classes = "menu_item";
if($this_page_id == $page->ID)
{
$classes .= " selected_menu";
}
?>
<div class="<?php echo $classes; ?>">
<div class="menu_item_picture">
<?php echo $page->post_title;?>
</div>
<?php
$list = wp_list_pages("title_li=&child_of=".$page->ID."&depth=2&echo=0");
if($this_page_id == $page->ID && strlen($list) >0)
{
?>
<ul>
<?php echo $list;?>
</ul>
<?php
}
?>
</div>
<?php
}
?>
Related
Here Is my question: What I am wanting To do is Take Results from a mysql table and turn them into a menu and a drop down menu
HERE IS A QUICK EXAMPLE:
if you see in my mysql table i have page_name and parent, So the example is:
page_name and if i have row 1 the page_name is 'Home' now it's parent is 'none' right but on id number 39 the page_name is 'Contact Us' and the Parent Is 'Far Far Away 123' so if the parent is equal to 'none' then it will show at the top of the menu not the drop down if it has a parent it will show under that parent like this:
Home | the ben page | The Brock Page | Far Far Away 123 | dsfk
Contact Us
You see Contact Us is under Far Far Away Because the parent Is Far Far Away 123
here is my table:
Here is my code That I am trying but it is not working for some reason:
<ul>
<?php
$sql = "SELECT * FROM pages ORDER by item_order";
$result = mysqli_query($db, $sql);
confirm_query($result);
while ($links = mysqli_fetch_assoc($result)) {
if($links['parent'] !== "none") {
?>
<li id = "<?php echo $links['id']; ?>"><a href="
<?php
echo "page.php?id=" . $links['id'] . "\" title=\"" . $links['page_title'] . "\"";
?>>
<?php
echo $links['page_name'];
?>
</a>
<?php
if($links['parent'] !== "none") {
$child = "";
$sql = "SELECT * FROM pages";
$result = mysqli_query($db, $sql);
while($row = mysqli_fetch_assoc($result)) {
if($row['parent'] !== "none") {
$child = $row['page_name'];
}
}
echo "<ul id=\"sub_menu\" class=\"sub_navagation" . $links['id'] . "\">";
echo "<li>";
echo $child;
echo "<li>";
echo "</ul>";
}
?>
</li>
<?php
}
}
?>
</ul>
CSS:
#sub_menu {
display: none;
}
#sub_menu:hover {
display: block;
}
Ok if as you can see i have the parent row in the MYSQL table and on id number 39 i want the 'Far Far Away123' to be the parent of Contact Us and i want to show it when i hover over 'Far Far Away123'
My suggestion is to build out an array of all the results. Then run through that array (instead of multiple database queries).
I added a function build_dropdown() that will take the page name and run through the array of pages to see if there are any items with a parent matching. If so, we make an array of those items and run through them to build the dropdown menu. If not, it does nothing and moves on to the next menu item.
<?php
function build_dropdown ($parent, $pages){
foreach($pages as $page){
if($page['parent'] == $parent){
$items = $page;
} // END if
} // END foreach
if(is_array($items)){ // If a sub
echo '<ul id="sub_menu" class="sub_navagation'. $item['id'] .'">';
foreach($items as $item){
echo '<li>'.$item['name'].'<li>';
} // END foreach
echo '</ul>';
} // END if
}
$sql = "SELECT * FROM pages ORDER by item_order";
$result = mysqli_query($db, $sql);
confirm_query($result);
while ($row = mysqli_fetch_assoc($result)) {
$pages[] = $row; // Add each row to $pages array to use later
}
foreach($pages as $key => $page){
if($page['parent'] == 'none'){ ?>
<li id = "<?php echo $page['id']; ?>">
<a href="page.php?id=<?php echo $page['id']; ?>" title="<?php echo $page['page_title']; ?>">
<?php echo $page['page_name']; ?>
</a>
<?php
build_dropdown($page['page_name'], $pages); // If there are child items then build them out
?>
</li>
<?php
} // END if
} // END foreach
?>
I suggest you will need to JOIN your table to basically query it again to get the parent value, and add that to your markup.
SELECT *
FROM Pages
LEFT JOIN Pages p2 on page_name = p2.parent
(note: the syntax above may not be right, but I wanted to give you an idea of where I would start).
I using Magento 1.7.2, and am showing a list of sub categories and sub-sub categories, from a current category. My problem is that the sub-sub categories do not respect the same order as in the admin backend tree structure. It looks like they are being ordered via their id, from smalled id number to highest. I would need them to show as they are ordered in the admin backend. Here is my current code:
<?php
$currCat = Mage::registry('current_category');
$parentname = $currCat->getName();
$collection = Mage::getModel('catalog/category')->getCategories($currCat->getEntityId());
$subcats = $currCat->getChildren();
$_helper = $this->helper('catalog/output');
echo '<h2 class="titleCat"><strong>'.$parentname.'</strong></h2>';
?>
<?php
$currCat = Mage::registry('current_category');
$parentname = $currCat->getName();
$collection = Mage::getModel('catalog/category')->getCategories($currCat->getEntityId());
$subcats = $currCat->getChildren();
$_helper = $this->helper('catalog/output');
echo '<h2 class="titleCat"><strong>'.$parentname.'</strong></h2>';
?>
<!-- We list sub sub categories -->
<div class="colLeftNav">
<ul class="colLeftSubCats">
<?php
foreach(explode(',',$subcats) as $subCatid)
{
$_category = Mage::getModel('catalog/category')->load($subCatid);
if($_category->getIsActive()) {
$sub_cat = Mage::getModel('catalog/category')->load($_category->getId());
$sub_subcats = $sub_cat->getChildren();
echo '<div class="subMainCat">'.$_category->getName().'</div>';
foreach(explode(',',$sub_subcats) as $sub_subCatid)
{
$_sub_category = Mage::getModel('catalog/category')->load($sub_subCatid);
if($_sub_category->getIsActive()) {
echo '<li class="subCat">'.$_sub_category->getName().'</li>';
}
}
}
}
?>
</ul>
</div>
I am pretty much stuck here and have no clue how to resolve this. Any help would be really appreciated...!
Something like this?
$cat_id = 10;
$category = Mage::getModel('catalog/category')->load($cat_id);
$collection = Mage::getModel('catalog/category')->getCategories($cat_id, 0, true, true);
foreach ($collection as $cat) {
echo $cat->getId().' '.$cat->getPosition().' '.$cat->getName().'<br/>';
}
I'm building a menu options, having issue in last option, The Anchor method doesn't work as a link popup a new window. Besides, in option 1 and 2, I repeat those codes which is not look great.
Is there a better way to optimize those codes? make it cleaner.
In my controller:
public function loadPage($name, $pageID) {
$data['title'] = $this->tabPageData;
$data['tabMenu'] = $this->model->getAllMenuItems();
if ($name == 'portfolio-1') {
// load portfolio 1, get the page content (photos) and its name
$data['tabPageContent'] = $this->model->getPageContentByPageID($pageID);
$data['pageName'] = $this->model->getPageNameByID($pageID);
} elseif ($name == 'portfolio-2') {
$data['tabPageContent'] = $this->model->getPageContentByPageID($pageID);
$data['pageName'] = $this->model->getPageNameByID($pageID);
} elseif ($name == 'contact') {
// load Contact page
$data['tabContact'] = $this->model->getContactByPageID($pageID);
} else {
// load a Blog site
echo anchor('http://mysite.tumblr.com', 'target=_blank');
}
$this->load->view('content', $data);
}
In my View:
<div id="menu">
<ul>
<?php foreach ($tabMenu as $item) : ?>
<?php
$url = "<li><a href='" . base_url();
$url .= str_replace("+", "-", urlencode(strtolower($item->name))) . "/". ($item->cat_id) . "'>";
$url .= strtoupper($item->name) . "</a></li>";
echo $url;
?>
<?php endforeach; ?>
</ul>
</div> <!-- end of Menu -->
I would suggest that you clean up your view by creating a helper method that generates a list item for your navigation.
Put the following code in a file named navigation_helper.php in application/helpers/.
if (!defined('BASEPATH')) exit('No direct script access allowed');
if (!function_exists('build_list_item'))
{
function build_list_item ($item) {
$url_item_name = str_replace('+', '-', urlencode(strtolower($item->name)));
$url = base_url() . $url_item_name . "/". $item->cat_id;
return '<li>' . strtoupper($item->name) . '</li>';
}
}
Make sure you are loading the helper in your controller or autoloading it if you use it often.
$this->load->helper('navigation_helper');
Then in your view you could do this:
<div id="menu">
<ul>
<?php foreach ($tabMenu as $item): ?>
<?php echo build_list_item($item); ?>
<?php endforeach; ?>
</ul>
</div>
I'm using following code to display my 3 level menu:
if(!$post->post_parent){
// will display the subpages of this top level page
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
}else{
// diplays only the subpages of parent level
//$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
if($post->ancestors) {
// now you can get the the top ID of this page
// wp is putting the ids DESC, thats why the top level ID is the last one
$ancestors = end($post->ancestors);
$children = wp_list_pages("title_li=&child_of=".$ancestors."&echo=0");
// you will always get the whole subpages list
}
}
if ($children) { ?>
<ul id="submenu">
<?php echo $children; ?>
</ul>
<?php } ?>
It lists pages in side bar, second level then 3rd level too. I would like to include very top level too so i would like to my structure to look as follow:
*A
-a
--a
-b
--b
-c
--c
Where as above code is not listing main page i.e. *A, i hope that make sense and someone will be able to help
Thanks,
I found this code snippet at the wordpress codex site, and I think it's exactly what you're looking for, I've pasted it in for convenience:
<?php
//if the post has a parent
if($post->post_parent){
//collect ancestor pages
$relations = get_post_ancestors($post->ID);
//get child pages
$result = $wpdb->get_results( "SELECT ID FROM wp_posts WHERE post_parent = $post->ID AND post_type='page'" );
if ($result){
foreach($result as $pageID){
array_push($relations, $pageID->ID);
}
}
//add current post to pages
array_push($relations, $post->ID); // <---- THIS IS INCLUDING THE PARENT
//get comma delimited list of children and parents and self
$relations_string = implode(",",$relations);
//use include to list only the collected pages.
$sidelinks = wp_list_pages("title_li=&echo=0&include=".$relations_string);
}else{
// display only main level and children
$sidelinks = wp_list_pages("title_li=&echo=0&depth=2&child_of=".$post->ID);
}
if ($sidelinks) { ?>
<h2><?php the_title() ;?></h2>
<ul>
//links in <li> tags
<?php echo $sidelinks; ?>
</ul>
<?php } ?>
It also has some built-in logic to not display everything if this is a highest-level page. Hope this helps!
<div id="breadcrumbs">
Home
<?php
$parent_id = $post->post_parent;
$breadcrumbs = array();
while ($parent_id) {
$page = get_page($parent_id);
$breadcrumbs[] = ''.get_the_title($page->ID).'';
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse($breadcrumbs);
foreach ($breadcrumbs as $crumb) echo ' / '.$crumb;
?>
</div>
<h1><?php the_title(); ?></h1>
credit : Ivovic
from : http://wordpress.org/support/topic/display-page-parent-on-page-with-title?replies=13
I am working on a Wordpress Theme, I need to work on the navigation, I am having a little trouble creating it.
The navigation I am looking for looks like this: www.neu.edu/humanities.
I have gotten this far:
if (is_front_page()) {
wp_list_pages('title_li=&exclude=12&depth=1');
}
else {
// display the subpages of the current page while
// display all of the main pages and all of the
// and display the parent pages while on the subpages
}
<?php
if (is_front_page()) {
wp_list_pages('title_li=&exclude12&depth=1');
}
else {
$output = wp_list_pages('echo=0&depth=1&title_li=&exclude=12');
if (is_page()) {
$page = $post-ID;
if ($post->post_parent) {
$page = $post->post_parent;
}
$children = wp_list_pages('echo=0&child_of='.$page.'&title_li=&exclude=12');
if ($children) {
$output = wp_list_pages('echo=0&child_of='.$page.'&title_li=&exclude=12');
}
}
echo $output;
}
?>