<?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>Valen Designs &#187; PHP</title>
	<atom:link href="http://valendesigns.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://valendesigns.com</link>
	<description>Build Market Empower</description>
	<lastBuildDate>Mon, 26 Jul 2010 12:07:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Is Page or Subpage WP Function</title>
		<link>http://valendesigns.com/wordpress/is-page-or-subpage-wordpress-function/</link>
		<comments>http://valendesigns.com/wordpress/is-page-or-subpage-wordpress-function/#comments</comments>
		<pubDate>Sat, 21 Mar 2009 12:47:13 +0000</pubDate>
		<dc:creator>Derek Herman</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[Child]]></category>
		<category><![CDATA[Functions]]></category>
		<category><![CDATA[Parent]]></category>

		<guid isPermaLink="false">http://valendesigns.com/?p=374</guid>
		<description><![CDATA[
Have you ever wanted to know if the page you&#8217;re on is a child of a certain page because you needed to know that information in order to add a snippet of code to the sidebar or remove something or whatever you had planned but you tried and just couldn&#8217;t figure out a solution that [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://valendesigns.com/wp-content/uploads/2009/03/is_page_or_sub.jpg" alt="Is Page or Subpage Function" title="Is Page or Subpage Function" width="460" height="120" class="alignnone size-full wp-image-388" /><br />
Have you ever wanted to know if the page you&#8217;re on is a child of a certain page because you needed to know that information in order to add a snippet of code to the sidebar or remove something or whatever you had planned but you tried and just couldn&#8217;t figure out a solution that was simple and easy to maintain? Well, here is a piece of code that will do just that. I use this very code all over the place, on pretty much every project I do. The concept is simple. I need to know more than if the is_page(&#8216;portfolio&#8217;); I need to know if this is_page_or_sub(&#8216;portfolio&#8217;); </p>
<p>This is pretty much the same function as is_page(), it lets you pass in a page ID or slug, your choice, but the only real difference is that it goes beyond a single page and looks for a family relationship. The function will return true for all children and grandchildren of the page id or slug you pass in. For example, you want to show two different sidebars, one for normal content and one for the portfolio section. You would use this function to do that. First, copy and paste this code into your functions.php and then take a look at the usage example below.</p>
<p>EDIT:<br />
You can get an updated version if this function and as a plugin if you got to <a href="http://valendesigns.com/news/added-plugin-to-wordpress-directory/">here</a>.</p>
<h3>The Function</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//If is page or subpage of $my_page, works with both ID or name.</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'is_page_or_sub'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">function</span> is_page_or_sub<span style="color: #009900;">&#40;</span><span style="color: #000088;">$my_page</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: #000088;">$wpdb</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$grand_parent</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_var</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT post_parent FROM <span style="color: #006699; font-weight: bold;">$wpdb-&gt;posts</span> WHERE ID = '&quot;</span><span style="color: #339933;">.</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: #0000ff;">&quot;' AND post_type = 'page'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// If you pass in a string, get the page ID of $my_page to use below</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$my_page</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$my_page</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_var</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT ID FROM <span style="color: #006699; font-weight: bold;">$wpdb-&gt;posts</span> WHERE post_name = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$my_page</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;' AND post_type = 'page'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #666666; font-style: italic;">// If this is $my_page or the child or grandchild of $my_page return true </span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> is_page<span style="color: #009900;">&#40;</span><span style="color: #000088;">$my_page</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</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;">$my_page</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$grand_parent</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$my_page</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> 
	<span style="color: #666666; font-style: italic;">// Else return false</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h3>Sample Usage</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>is_page_or_sub<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'portfolio'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
    DISPLAY PORTFOLIO SIDEBAR
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    DISPLAY REGULAR SIDEBAR
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>If you have any question feel free to comment below. Have fun taking over the internet one WordPress blog at a time.</p>
<h3>Donations</h3><ul class="notebook-donate"><li>Make a <a class="donateLink" href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=accounts@valendesigns.com&item_name=Donation+for+Code+Snippet" rel="external">donation</a> and help keep the awesomeness flowing.</li></ul> <h3>Disclaimer</h3><div class="disclaimer"><p>Valen Designs grants you a nonexclusive copyright license to use all programming code examples from which you can generate similar function tailored to your own specific needs.</p> <p>Valen Designs, cannot guarantee or imply reliability, serviceability, or function of these programs.</p> <p>All programs contained herein are provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.</p></div>
]]></content:encoded>
			<wfw:commentRss>http://valendesigns.com/wordpress/is-page-or-subpage-wordpress-function/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
	</channel>
</rss>
