I've been trying to set up a basic sidebar in Wordpress that has a couple conditions.
If it's a top level page, display the first level of children
If it's a child page, display the parent as well as its siblings
I'm getting some results with this, but it's adding pages that aren't directly children.
<?php
if($post->post_parent)
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
else
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
if ($children) { ?>
<?php echo '<h4>Explore ' . get_the_title($parent[1]) . '</h4>'; ?>
<?php echo $children; ?>
There are 2 parts to this question
Limit the children to just 1 level: You can pass depth into wp_list_pages(), to can specify the level of hierarchy.
If its a child page, include the parent - but only the parent and not its siblings - in the list.
To add the parent to the list, you need to do things a little differently - you must first compile a a list of the ids of all the pages you want to get, and pass that into wp_list_pages.
The following code is untested, but the logic should be right:
if($post->post_parent){
// get a list of all the children of the parent page
$pages = get_pages(array('child_of'=>$post->post_parent));
if ($pages) {
// get the ids for the pages in a comma-delimited string
foreach ($pages as $page)
$page_ids[] = $page->ID;
$siblings = implode(',',$page_ids);
// $pages_to_get is a string with all the ids we want to get, i.e. parent & siblings
$pages_to_get = $post->post_parent.','.$siblings;
// use "include" to get only the pages in our $pages_to_get
$children = wp_list_pages("include=".$pages_to_get."&echo=0");
}
}
else{
// get pages that direct children of this page: depth=1
$children = wp_list_pages("title_li=&child_of=".$post->ID."&depth=1&echo=0");
}
// display the children:
if ($children) {
echo '<h4>Explore ' . get_the_title($parent[1]) . '</h4>';
echo $children;
}
?>
Related
I'm trying to print only the parent category for each post and a permalink to that category within a custom Wordpress RSS feed.
Using <?php the_category_rss(); ?> prints all categories for a post (parent and child categories) like this:
<category><![CDATA[Category 1]]></category>
<category><![CDATA[Category 2]]></category>
<category><![CDATA[Category 3]]></category>
I'd like to print only the parent categories for a post rather than parent and child categories.
I'm also looking for the proper way to print the parent category's permalink within the RSS feed.
I have a feeling the way to do this is a filter, but I'm not sure how to implement it. This is what I have right now, but it returns a 1 instead of the parent category name:
add_filter('the_category_rss', 'only_parent_rss_categories');
function only_parent_rss_categories( $allparents ) {
$categories = get_the_category();
$category = $category->category_parent == 0;
$allparents= esc_html("<category><![CDATA[{$category}]]></category>");
return $allparents;
}
I'm still interested in hearing how this can be accomplished using the Wordpress function the_category_rss(), but I did find another way to do this:
<?php $parentsonly = "";
// loop through categories
foreach((get_the_category()) as $category) {
// exclude child categories
if ($category->category_parent == 0) {
$parentsonly = '<category domain="' . get_category_link($category->cat_ID) . '">' . $category->name . '</category>';
}
}
echo $parentsonly; ?>
This returns each parent category in the proper RSS validated format, using <category> and domain for the category URL:
<category domain="http://www.yourdomain.com/category-archive/">My Category</category>
Hope this helps someone else!
I'm trying to dynamically target different levels of a taxonomy that are setup like this:
Top Level Category
-- Second Level Category
--- Third Level Category
I've targeted the first and second levels like this:
<?php $parent = get_queried_object()->parent;
if($parent == "0"){ ?>
Top level
<?php } else { ?>
Second level
<?php } ?>
How can I target the third level?
<?php $term_id = get_queried_object()->term_id;
$ancestors = get_ancestors( $term_id, 'categories' ); ?>
<pre>
<?php print_r($ancestors); ?>
</pre>
When on a category page with the code above... top level shows the array as empty, second level shows the id of the parent, third level shows the array with two levels. How can I target each individual level?
Try with get_ancestors() WP function : get_ancestors()
function get_tax_level($id, $tax){
$ancestors = get_ancestors($id, $tax);
return count($ancestors)+1;
}
$current_term_level = get_tax_level(get_queried_object()->term_id, get_queried_object()->taxonomy);
if ($current_term_level = 0) {
// show first drop-down
} else if ($current_term_level = 1) {
// show second drop-down
} else {
// show third drop-down
}
I have a navigation structure like this:
Home
About
The Team
Our Mission
Locations
Chicago
New York
Los Angeles
Services
Contact Us
If I'm looking at the About page, I want the sidebar to display:
The Team
Our Mission
If I'm looking at the Services page, I want the sidebar to display:
Home
About
Locations
Services
Contact Us
This is the code I'm currently using. How can I modify it so that it works the way I want?
<?php
global $wp_query;
if( empty($wp_query->post->post_parent) ) {
$parent = $wp_query->post->ID;
} else {
$parent = $wp_query->post->post_parent;
}
$children = get_pages('child_of='.$parent);
if( count( $children ) > 0 ) {
?>
<ul>
<?php wp_list_pages ("&title_li=&child_of=$parent"); ?>
</ul>
<?php } ?>
this should either do it outright or at least show you how:
when viewing a Page that has children (or is a child) it displays only children of that parent.
When visiting main page, all top level pages are listed in the sidebar.
When visiting a top level page with no children, all top level pages are listed.
When visiting a top level page with children, just the children pages, and descendant pages, are listed.
When visiting a child page, just the children, and descendant pages, of that parent, are listed.
<?php
$output = wp_list_pages('echo=0&depth=1&title_li=<h2>Top Level Pages </h2>' );
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=' );
if ($children) {
$output = wp_list_pages ('echo=0&child_of=' . $page . '&title_li=<h2>Child Pages</h2>');
}
}
echo $output;
?>
from: http://codex.wordpress.org/Function_Reference/wp_list_pages#List_subpages_even_if_on_a_subpage
This method takes advantage of the echo=0 parameter, which returns the results as a string to a variable. The if ($children) test now works because wp_list_pages() will return empty if it finds no matches.
I need some help doing a navigation for my site but I couldn't figure out how to do it using Wordpress' wp_list_pages() function.
For reference, the navigation system on http://www.rwgroup.com/ should give the exact idea of what I am aiming for.
Before I tried using wp_list_pages('child_of='.$post->parent_post) but it seems that the pages only display wrt it's immediate parent and forgotten it's ancestors. I've been trying this out for a while now but I just couldn't get my head around it.
Any ideas would be great
Try this code to list out current page with its ancestors and children-
<?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);
//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=1&child_of=".$post->ID);
}
if ($sidelinks) { ?>
<h2><?php the_title(); ?></h2>
<ul>
<?php //links in <li> tags
echo $sidelinks; ?>
</ul>
<?php } ?>
I'm trying to build some code on my magento site that will allow the customer to click through my product categories viewing the child categories on each consecutive page.
I have a code snippet below that will successfully works for the first 2 levels, but does not allow me to browse the childern of the second level (i.e.the 3rd level etc)
Can anyone assist in tweaking this code to allow me to browse 4 levels deep?
<?php
$obj = new Mage_Catalog_Block_Navigation();
$store_cats = $obj->getStoreCategories();
$current_cat = $obj->getCurrentCategory();
$current_cat = (is_object($current_cat) ? $current_cat->getName() : '');
foreach ($obj->getCurrentChildCategories() as $subcat) {
echo '<li>'.$subcat->getName()."</li>\n";
}
echo "</ul>\n</li>\n";
?>
You need to add recursively the $obj->getCurrentChildCategories() for the child categories aswell, since your current foreach only iterates for the childs of the first one.
Store the foreach in a function and use it for the $cat object.
function cats($obj) {
foreach ($obj->getCurrentChildCategories() as $subcat) {
echo '<li>'.$subcat->getName()."</li>\n";
cats($subcat);
}
}