Created
April 25, 2012 00:55
-
-
Save LogixRepo/2485040 to your computer and use it in GitHub Desktop.
Wordpress: Is Child Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Checks if the page is the parent or a child of the page matching the $pid param | |
| * $pid can be the page slug or the page ID # | |
| * | |
| * This function is best used to display menus when the parent/top level menu item needs | |
| * a rollover state or special treatment. | |
| * | |
| * @param \$pid (string or int) | |
| * returns true if current page ID matches $pid, else return false | |
| */ | |
| function is_child( \$pid ) { // $pid = The ID of the page we're looking for pages underneath | |
| global \$post; // load details about this page | |
| // if \$pid is an ID | |
| if (is_int(\$pid)) { | |
| if ( is_page(\$pid) ) | |
| return true; | |
| // get the page's children if the ID of a child matches the $pid return true | |
| \$anc = get_post_ancestors( \$post->ID ); | |
| foreach ( \$anc as \$ancestor ) { | |
| if( is_page() && \$ancestor == \$pid ) { | |
| return true; | |
| } | |
| } | |
| } | |
| // if $pid is a page slug | |
| elseif (is_string(\$pid)) { | |
| // Get page by 'SLUG' | |
| \$page = get_page_by_path( \$pid ); | |
| if (\$post->ID == \$page->ID) { | |
| return true; | |
| } | |
| \$anc = get_post_ancestors( \$post->ID ); | |
| foreach ( \$anc as \$ancestor ) { | |
| if( is_page() && $ancestor == \$page->ID ) { | |
| return true; | |
| } | |
| } | |
| } | |
| return false; // we arn't at the page, and the page is not an ancestor | |
| } | |
| /* | |
| Used in a template: | |
| <img src="home<?php if (is_child(0)){?>-ro<?php } ?>.jpg"> | |
| or | |
| <img src="home<?php if (is_child('home')){?>-ro<?php } ?>.jpg"> | |
| if true equals | |
| <img src="home-ro.jpg"> | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment