<?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>webhank &#187; example code</title>
	<atom:link href="http://www.webhank.com/tag/example-code/feed" rel="self" type="application/rss+xml" />
	<link>http://www.webhank.com</link>
	<description>my two cents - by Hank Pantier</description>
	<lastBuildDate>Wed, 04 Aug 2010 19:42:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Getting Root ID from embeded pages</title>
		<link>http://www.webhank.com/web-development/getting-root-id-from-embeded-pages?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=getting-root-id-from-embeded-pages</link>
		<comments>http://www.webhank.com/web-development/getting-root-id-from-embeded-pages#comments</comments>
		<pubDate>Thu, 17 Apr 2008 17:20:47 +0000</pubDate>
		<dc:creator>webhank</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[example code]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.webhank.com/?p=64</guid>
		<description><![CDATA[For quite a while now I have been using the following script to show sub navigation on WordPress Sidebars. It utilizes the built in wp_list_pages function to show parent and child relationships on a sidebar (or where ever you choose to use it for that matter). &#60;?php if($post-&#62;post_parent) $children = wp_list_pages("title_li=&#38;child_of=".$post-&#62;post_parent."&#38;echo=0"); else $children = wp_list_pages("title_li=&#38;child_of=".$post-&#62;ID."&#38;echo=0"); [...]]]></description>
			<content:encoded><![CDATA[<pre>
<pre>
<pre><a href="http://www.webhank.com/wp-content/uploads/wordpressicon-hanttula2.gif"><img class="alignright size-full wp-image-61" style="float: right;" title="wordpressicon-hanttula2" src="http://www.webhank.com/wp-content/uploads/wordpressicon-hanttula2.gif" alt="wordpressicon hanttula2 Getting Root ID from embeded pages" width="80" height="80" /></a></pre>
</pre>
</pre>
<p>For quite a while now I have been using the following script to show sub navigation on <a title="Customizing Your WordPress Sidebar" href="http://codex.wordpress.org/Customizing_Your_Sidebar" target="_blank">WordPress Sidebars</a>.  It utilizes the built in <a title="Wp List Pages link to codex" href="http://codex.wordpress.org/Template_Tags/wp_list_pages" target="_blank">wp_list_pages</a> function to show parent and child relationships on a sidebar (or where ever you choose to use it for that matter).</p>
<p><tt> </tt>&lt;?php</p>
<pre><tt>if($post-&gt;post_parent)
$children = wp_list_pages("title_li=&amp;child_of=".$post-&gt;post_parent."&amp;echo=0");
else $children = wp_list_pages("title_li=&amp;child_of=".$post-&gt;ID."&amp;echo=0");</tt> <tt>if ($children) { ?&gt;</tt> <tt> &lt;ul&gt; &lt;?php echo $children; ?&gt; &lt;/ul&gt; &lt;?php } ?&gt;</tt></pre>
<p>I like that it is simple, and that it can be re-written to show items only if they are child pages of a specific parent, ie:</p>
<pre>&lt;?php
// find out what the top level page is

global $post;
if($post-&gt;post_parent) $parentPG = $post-&gt;post_parent; // page is a child
else $parentPG = $post-&gt;ID; // page has children
?&gt;

&lt;?php

// marketing

if($parentPG==7){

// show items if the parent id is 7
}elseif($parentPG==8){
// show items if the parent id is 8
}
?&gt;</pre>
<p>This works great when you only need to make it work with parent and child pages.  Recently, however, I had the need to utilize this type of functionality on a project that required pages 3 levels deep (grandchildren?).  After a bit of Googling, and searching through the <a title="Link to the WordPress Codex" href="http://codex.wordpress.org/Main_Page" target="_blank">WordPress Codex</a> I realized that I was going to have to &#8220;roll my own&#8221; on this one.  So here you go &#8211; there are probably better ways of doing this.  If you know of one please let me know, but maybe this will help someone else searching for &#8220;wordpress list grandchildren&#8221;</p>
<pre>&lt;?php
// find out what the top level page is
global $post;
if($post-&gt;post_parent){
$parentPG = $post-&gt;post_parent; // page is a child

// check for grand children
$sql = "SELECT * FROM `".$wpdb-&gt;prefix."posts` WHERE ID=$parentPG";
$data = $wpdb-&gt;get_results($sql,ARRAY_A);

if(!empty($data[0][post_parent])) $parentPG=$data[0][post_parent];
}
else $parentPG = $post-&gt;ID; // page has children
?&gt;

&lt;?php

// marketing
if($parentPG==7){
// show stuff for parent 7
}elseif($parentPG==8){
// show stuff for parent 8
}
?&gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.webhank.com/web-development/getting-root-id-from-embeded-pages/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

