How to target different levels of a Wordpress taxonomy? - php

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
}

Related

Displaying first level parent and co-children of that parent page

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

setting for child category on single.php wordpress

can any one help me or translate what I want in php:
I need php code like that:
If child categories posts from the parent category number (1) {
will include templatepath'/single-Arabic.php
}
else if child categories posts from the parent category number (2) {
will include templatepath'/single-English.php
}
best regards
It's better to check for category name rather than category ID. So lets say you have two categories, "fruit" and "meat", then in your single.php just after the get_header(); call, add these lines:
if(has_term('fruit', 'category', $post)) get_template_part('single', 'fruit');
else if(has_term('meat', 'category', $post)) get_template_part('single', 'meat');
// else...
This will work if you've created the files single-fruit.php and single-meat.php
More here: https://codex.wordpress.org/Function_Reference/get_template_part
here is the function to get top parent category.
let suppose you have category
1 . English -- sub 1 English category -- sub 2 English
category
2 . Arabic -- sub 1 Arabic category -- sub 2 Arabic
category
and you are on the post page where it say current category sub 2 English
then you call function
$Parent = strtolower(get_top_category());
it will return top category name which is English, after that condition will check if category is English then include 'single-English.php'
vice versa for Arabic.
function get_top_category() {
$category = get_the_category();
$cat_tree = get_category_parents($category[0]->term_id, FALSE, ':', TRUE);
$top_cat = split(':',$cat_tree);
return $parent = $top_cat[0];
}
echo '<pre>';print_r(get_top_category());echo '</pre>';
$Parent = strtolower(get_top_category());
if($Parent == "english") {
include 'templatepath/single-English.php';
}
else if($Parent == "arabic") {
include 'templatepath/single-Arabic.php';
}

List children pages, unless none exist, in which case, list sibling pages, in WordPress

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.

Check if a page is a parent or if it's a child page?

Is it possible to check if a page is a parent or if it's a child page?
I have my pages set up like this:
-- Parent
---- Child page 1
---- Child page 2
etc.
I want to show a certain menu if it's a parent page and a different menu if it's on the child page.
I know I can do something like below but I want to make it a bit more dynamic without including specific page ID's.
<?php
if ($post->post_parent == '100') { // if current page is child of page with page ID 100
// show image X
}
?>
You can test if the post is a subpage like this:
*(from http://codex.wordpress.org/Conditional_Tags)*
<?php
global $post; // if outside the loop
if ( is_page() && $post->post_parent ) {
// This is a subpage
} else {
// This is not a subpage
}
?>
Put this function in the functions.php file of your theme.
function is_page_child($pid) {// $pid = The ID of the page we're looking for pages underneath
global $post; // load details about this page
$anc = get_post_ancestors( $post->ID );
foreach($anc as $ancestor) {
if(is_page() && $ancestor == $pid) {
return true;
}
}
if(is_page()&&(is_page($pid)))
return true; // we're at the page or at a sub page
else
return false; // we're elsewhere
};
Then you can use it:
<?php
if(is_page_child(100)) {
// show image X
}
?>
I know this is an old question but I was searching for this same question and couldn't find a clear and simple answer until I came up with this one. My answer doesn't answer his explanation but it answers the main question which is what I was looking for.
This checks whether a page is a child or a parent and allows you to show, for example a sidebar menu, only on pages that are either a child or a parent and not on pages that do not have a parent nor children.
<?php
global $post;
$children = get_pages( array( 'child_of' => $post->ID ) );
if ( is_page() && ($post->post_parent || count( $children ) > 0 )) :
?>
For Wordpress, you can simply check:
<?php
if (wp_get_post_parent_id(get_the_ID())) {
echo "I am a child page";
}
?>
You can use the get_pages() function. it takes an associative array as an argument. you can give that array 'child_of' => get_the_ID() to get the children of the current page, and if it hasn't any children the whole get_pages() function will return false, otherwise it will return a value that evaluates to true, which can be assigned to a variable to use as a conditional in an if statement.
$iAmParent = get_pages(array('child_of' => get_the_ID()));

Magento - browse 4 tier category tree

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);
}
}

Categories