Skip to content

Instantly share code, notes, and snippets.

@germanny
Last active August 29, 2015 13:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save germanny/9399399 to your computer and use it in GitHub Desktop.
Save germanny/9399399 to your computer and use it in GitHub Desktop.
why does this fail?
<?php
/**
* Get a Page's ID by post_name (slug)
*/
function get_id_by_page_name($post_name) {
global $wpdb;
$post_name_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$post_name."'");
if ( ( $post_name_id !== NULL ) || ( $post_name_id !== '0' ) ) {
return $post_name_id;
}
}
/* use like this:
*
* if ( get_id_by_page_name('schools') == $post->post_parent ) { do stuff }
*/
?>
<?php
get_header();
// check and apply page template if it's one of these pages
if ( is_front_page() ) : include_once('templates/tmpl-home.php');
elseif ( get_id_by_page_name('subjects') == $post->post_parent ) : include_once('templates/tmpl-subjects-child.php');
elseif ( 'subjects' == get_absolute_ancestor($post->ID) && !is_page('subjects') ) : include_once('templates/tmpl-subjects-grandchild.php');
elseif ( get_id_by_page_name('schools') == $post->post_parent ) : include_once('templates/tmpl-schools-child.php');
elseif ( is_page('school-profile') ) : include_once(CHILD_SS_DIR . '/templates/tmpl-schools-child.php');
// else use regular page template
else {
// do stuff
}
?>
@ericrasch
Copy link

Your page.php can be completely cleaned up by using both the is_tree and is_child functions from here: https://gist.github.com/ericrasch/4723316

Also, you should make your include_once(CHILD_SS_DIR . '/ uniform to use the template path as you did in line 9: https://gist.github.com/germanny/9399399#file-page-php-L9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment