kevinleary.net

WordPress is_child() for Advanced Navigation

Word­Press lacks one key fea­ture for advanced devel­op­ers using it as a CMS plat­form. That fea­ture is the is_child() (or would be) func­tion. If your famil­iar with Word­Press Theme Devel­op­ment you have prob­a­bly ran into the fol­low­ing Con­di­tional Tags from time to time:

  • is_home()
  • is_front_page()
  • is_single()
  • is_sticky()
  • is_page()

If you ever want to have a side­bar nav­i­ga­tion dis­play the next level of child pages, you would need is_child() to do so.

// is_child()
function is_child($parent) {
	global $wp_query;
	if ($wp_query->post->post_parent == $parent) {
		$return = true;
	}
	else {
		$return = false;
	}
	return $return;
}

Usage

To use the con­di­tional in your theme(s), add the code above to functions.php. This only takes the Post IDas an argu­ment, so you’ll need to use some­thing like is_child(201); because is_child(‘Services’); will not work.

Work­ing Example

The inner Ser­vices pages on kevinleary.net use this func­tion to cre­ate a dynamic bread­crumb. I know there are plu­g­ins for this but I was look­ing for some­thing sim­ple and quick.

<?php if(is_child(221)) { ?>
		<h1 class="post-title"><a href="/services/">Services</a> <em>&gt;</em> <?php the_title(); ?></h1>
<?php } else { ?>
		<h1 class="post-title"><?php the_title(); ?></h1>
<?php } ?>

Spe­cial thanks goes out to Nicholas Rous­sos for cre­at­ing this won­der­ful gem.

is_ancestor() func­tion for deeper functionality

<?php
function is_ancestor($post_id) {
	global $wp_query;
	$ancestors = $wp_query->post->ancestors;
	if ( in_array($post_id, $ancestors) ) {
		$return = true;
	} else {
		$return = false;
	}
	return $return;
}
?>

By pop­u­lar demand, I’ve added a more in depth way to check if a post is a child OR an ances­tor of a spe­cific page ID. This hasn’t been tested yet, if it doesn’t work let me know and I can fix it.

8 Comments

  1. JTL / 5.19.09 / 2:39 PM

    try copy and paste again buddy — your code is chalk full o errors!

  2. kevin / 5.20.09 / 6:27 AM

    All fixed, looks like I for­got to add the open­ing and clos­ing PHP tags.

  3. Natawa / 7.17.09 / 11:42 AM

    Прив, Ваш сайт прикольный хотела узнать на каком движке делали? и смогу ли я что-то подобное сделать на Ворд Пресе?

  4. Anton / 7.27.09 / 11:39 AM

    Как узнать пароль , если раньше был у Вас на сайте зарегестрирован?

  5. Katya / 7.28.09 / 6:58 AM

    Ребята, привет, а реально такой сайт как у Вас написать в штмл?. у меня знание посредственные..

  6. kevinlearynet / 7.28.09 / 12:02 PM

    Sorry Natawa, I’m an eng­lish only kind of guy. What lan­guage is that?

    I’ll see if I can have it or pos­si­bly the site auto trans­lated for you.

  7. Olegreze / 9.9.09 / 3:59 PM

    Основная задача Яндекса — давать ответы на вопросы пользователей!

  8. Петрович / 11.28.09 / 7:55 AM

    Впечатлило, однако!

Leave a comment

will not be published

Wrap code blocks with <pre lang="LANGUAGE" line="1"> and </pre> where LANGUAGE is a GeSHi supported language syntax.