example usage: examples.php
-
-
Save germanny/9399616 to your computer and use it in GitHub Desktop.
This file contains 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
<?php | |
if ( is_tree( 'subpage' ) ) { // Using the slug /subpage/, but you could also use the ID of /subpage/ | |
// [[... insert your code here ...]] | |
} | |
if ( is_child('subjects') ) { | |
//do something | |
} | |
$page_id = get_id_by_slug('resources'); | |
?> |
This file contains 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
<?php | |
/** | |
* Get a Page's ID by slug | |
* http://erikt.tumblr.com/post/278953342/get-a-wordpress-page-id-with-the-slug | |
*/ | |
function get_id_by_slug($page_slug) { | |
$page = get_page_by_path($page_slug); | |
if ($page) { | |
return $page->ID; | |
} else { | |
return null; | |
} | |
} | |
?> |
This file contains 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
<?php | |
/** | |
* BEGIN: Check If Page Is Child | |
* Source: http://bavotasan.com/2011/is_child-conditional-function-for-wordpress/ | |
*/ | |
function is_child( $page_id_or_slug ) { // $page_id_or_slug = The ID of the page we're looking for pages underneath | |
global $post; // load details about this page | |
if ( !is_numeric( $page_id_or_slug ) ) { // Used this code to change a slug to an ID, but had to change is_int to is_numeric for it to work. | |
$page = get_page_by_path( $page_id_or_slug ); | |
if (isset($page)) { | |
$page_id_or_slug = $page->ID; | |
if ( is_page() && ( $post->post_parent == $page_id_or_slug ) ) | |
return true; // we're at the page or at a sub page | |
else | |
return false; // we're elsewhere | |
} else { | |
return false; | |
} | |
} | |
}; | |
?> |
This file contains 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
<?php | |
/** | |
* Check If Page Is Parent/Child/Ancestor | |
* Source: http://css-tricks.com/snippets/wordpress/if-page-is-parent-or-child/#comment-172337 | |
*/ | |
function is_tree( $page_id_or_slug ) { // $page_id_or_slug = The ID of the page we're looking for pages underneath | |
global $post; // load details about this page | |
if ( !is_numeric( $page_id_or_slug ) ) { // Used this code to change a slug to an ID, but had to change is_int to is_numeric for it to work: http://bavotasan.com/2011/is_child-conditional-function-for-wordpress/ | |
$page = get_page_by_path( $page_id_or_slug ); | |
$page_id_or_slug = $page->ID; | |
} | |
if ( is_page() && ( $post->post_parent == $page_id_or_slug || (is_page( $page_id_or_slug ) || in_array($page_id_or_slug, $post->ancestors) ) ) ) | |
return true; // we're at the page or at a sub page | |
else | |
return false; // we're elsewhere | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment