<?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>Christian Schenk&#187; parent</title>
	<atom:link href="http://www.christianschenk.org/blog/tag/parent/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.christianschenk.org</link>
	<description>Writing about my experiences with technology and all different kinds of projects and experiments</description>
	<lastBuildDate>Sun, 29 Aug 2010 09:08:16 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>WordPress: is_child function</title>
		<link>http://www.christianschenk.org/blog/wordpress-is_child-function/</link>
		<comments>http://www.christianschenk.org/blog/wordpress-is_child-function/#comments</comments>
		<pubDate>Sun, 20 Dec 2009 06:10:48 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[is_child]]></category>
		<category><![CDATA[is_tree]]></category>
		<category><![CDATA[parent]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/?p=735</guid>
		<description><![CDATA[Every once in a while I would like to test whether the current page is a descendant of another page. This is particularly useful if you want to display certain content on a subset of your pages only. Although the Codex suggests a similar is_tree function I would like to present an extended version here.
I&#8217;ve [...]]]></description>
			<content:encoded><![CDATA[<p>Every once in a while I would like to test whether the current page is a descendant of another page. This is particularly useful if you want to display certain content on a subset of your pages only. Although the <a href="http://codex.wordpress.org/">Codex</a> suggests a similar <code>is_tree</code> function I would like to present an extended version here.</p>
<p>I&#8217;ve packaged the functionality inside a WordPress plugin called is_child that you can download <a href="http://data.christianschenk.org/wordpress-is_child-function/ischild.php.zip">here</a>. Unzip it, upload it to <code>wp-content/plugins</code> and activate the plugin.</p>
<p><span id="more-735"></span></p>
<h2>Coding is_child</h2>
<p>Doing a quick web search brings up tons of sites providing some sort of <code>is_child</code> or <code>is_tree</code> function. Most of the time it&#8217;s something like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> is_child<span style="color: #009900;">&#40;</span><span style="color: #000088;">$parent</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$post</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_parent</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$parent</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is great and a pretty nice solution. I&#8217;ve taken this approach and extended it with the following functionality:</p>
<ul>
<li>by default we&#8217;ll retrieve all parent pages up to the root node and test whether the current page is part of this tree. This behavior can be disabled though.</li>
<li>you can supply the name (<em>slug</em>) of a post/page instead of the ID and the function will look it up for you.</li>
</ul>
<p>All this combined with some error checking makes a great idea even better. Just have a look at the code if you would like to know more about the exact implementation.</p>
<h2>Conclusion</h2>
<p>We&#8217;ve seen that it&#8217;s very easy to test whether the current page is a child of another page or part of a certain tree in your page hierarchy. Packaging this into a plugin or placing the necessary code into your theme&#8217;s <code>functions.php</code> allows you to display certain content on particular pages only. In case you&#8217;re using the <a href="http://wordpress.org/extend/plugins/widget-logic/">Widget logic</a> plugin you now can display widgets on pages belonging to a particular subtree of your pages only, which is great.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/wordpress-is_child-function/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
