<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>wordpress taxonomies &#8211; Blackbams Blog</title>
	<atom:link href="https://blog.blackbam.at/tag/wordpress-taxonomies/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.blackbam.at</link>
	<description>development - digital arts - internet</description>
	<lastBuildDate>Mon, 24 Oct 2011 21:46:52 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.1</generator>
	<item>
		<title>Determine if a WordPress page or category is in the current ancestor/child tree (inside/outside the loop)</title>
		<link>https://blog.blackbam.at/2011/10/08/determine-if-a-wordpress-page-or-category-is-in-the-current-ancestorchild-tree-insideoutside-the-loop/</link>
					<comments>https://blog.blackbam.at/2011/10/08/determine-if-a-wordpress-page-or-category-is-in-the-current-ancestorchild-tree-insideoutside-the-loop/#comments</comments>
		
		<dc:creator><![CDATA[Blackbam]]></dc:creator>
		<pubDate>Sat, 08 Oct 2011 20:00:53 +0000</pubDate>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WP Scripts]]></category>
		<category><![CDATA[ancestor/child]]></category>
		<category><![CDATA[wordpress categories]]></category>
		<category><![CDATA[wordpress inheritance]]></category>
		<category><![CDATA[wordpress taxonomies]]></category>
		<guid isPermaLink="false">https://blog.blackbam.at/?p=1722</guid>

					<description><![CDATA[It is a common problem to detect parents or childs of pages and categories in WordPress correctly. The way WordPress stores relationships in hierarchies is not that trivial sometimes, so the results when querying are a bit confusing sometimes. It took some time to determine and optimize functions which do this job good and without faults, and I want to present these. As in this function I prefer working with page IDs, as this is the savest way to identify certain pages. With little changes it should also be possible to use these using e.g. slugs. Both of these functions can be used inside and outside of "the Loop" or loops.]]></description>
										<content:encoded><![CDATA[<p>It is a common problem to detect parents or childs of pages and categories in WordPress correctly. The way WordPress stores relationships in hierarchies is not that trivial sometimes, so the results when querying are a bit confusing sometimes. It took some time to determine and optimize functions which do this job good and without faults, and I want to present these. As in this function I prefer working with page IDs, as this is the savest way to identify certain pages. With little changes it should also be possible to use these using e.g. slugs. Both of these functions can be used inside and outside of &#8220;the Loop&#8221; or loops.</p>
<h2>Check if the current page (or a given page) is in the child tree of a given page</h2>
<p>This function checks if the current page (or a given page, second parameter) is in the child tree of a given page.</p>
<pre lang="php">
/* Tests, if a page has got a certain parent anywhere in the ancestor/children tree */
function is_tree($pid, $pid2check = 0) {// $pid = The ID of the page we're looking for pages underneath
 
	global $post;
	// load details about this page
 
	// check if we should check two posts independently from the current query
	if ($pid2check < 1) {
		$pid2check = $post -> ID;
	}
 
	$anc = get_post_ancestors($pid2check);
 
	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
	}
}
</pre>
<h2>Check if the current post (or a given post) is in the ancestor/child tree of a given category</h2>
<p>This function checks if the current post (or a given post identified by ID, second parameter) is in the ancestor/child tree of a given category.</p>
<pre lang="php">
/* Tests, if the given category is the current category, or in the ancestor/child tree of a given post or a child of it */
function is_category_or_sub($cat_id = 0, $pid = 0) {
	global $post;
 
	// check if a custom post id was given or get default (standard)
	if($pid<1) {
		$pid=$post->ID;
	}
 
	// check it for each category
    foreach (get_the_category($pid) as $cat) {
    	if ($cat_id == $cat->cat_ID || cat_is_ancestor_of($cat_id, $cat))  {
    		return true;
        }
    }
    return false;
}
</pre>
<h2>Check if a given category is in the current category tree</h2>
<p>Helpful if you have to determine the current category manually &#8211; checks if a given category is in the tree of another given category.</p>
<pre lang="php">
function in_category_tree($parent_cat,$child_or_equal_cat) {
	if($parent_cat ==$child_or_equal_cat || cat_is_ancestor_of($parent_cat, $child_or_equal_cat)) {
		return true;
	}
	return false;
}
</pre>
<h2>Custom Taxonomy hierarchy check</h2>
<p>Determine the top level taxonomy or check if a taxonomy is in the current taxonomies hirarchical tree.</p>
<pre lang="php">
function is_taxonomy_or_sub($child_or_equal_term_id,$parent_term_id,$taxonomy_name) {
       if($parent_term_id==$child_or_equal_term_id || $parent_term_id == get_top_level_term($child_or_equal_term_id,$taxonomy_name))  {
                return true;
        }
 }
 
function get_top_level_term($term_id,$taxonomy_name) {
	$term = get_term_by('id',$term_id,$taxonomy_name);
	if($term->parent>0) {
		return get_top_level_term($term->parent,$taxonomy_name);
	} else {
		return $term->term_id;
	}
}
</pre>
<p>This article is still under construction. Please report bugs, errors or improvement ideas, so we can optimize these scripts.</p>
<p><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fblog.blackbam.at%2F2011%2F10%2F08%2Fdetermine-if-a-wordpress-page-or-category-is-in-the-current-ancestorchild-tree-insideoutside-the-loop%2F&#038;title=Determine%20if%20a%20WordPress%20page%20or%20category%20is%20in%20the%20current%20ancestor%2Fchild%20tree%20%28inside%2Foutside%20the%20loop%29" data-a2a-url="https://blog.blackbam.at/2011/10/08/determine-if-a-wordpress-page-or-category-is-in-the-current-ancestorchild-tree-insideoutside-the-loop/" data-a2a-title="Determine if a WordPress page or category is in the current ancestor/child tree (inside/outside the loop)"><img src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded>
					
					<wfw:commentRss>https://blog.blackbam.at/2011/10/08/determine-if-a-wordpress-page-or-category-is-in-the-current-ancestorchild-tree-insideoutside-the-loop/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
