Last active
October 23, 2023 14:22
-
-
Save prosvitco/7e966225043f33399ce8cd6cf95fe71b to your computer and use it in GitHub Desktop.
Wp_Query is_page
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
public function is_page( $page = '' ) { | |
if ( ! $this->is_page ) { | |
return false; | |
} | |
if ( empty( $page ) ) { | |
return true; | |
} | |
$page_obj = $this->get_queried_object(); | |
if ( ! $page_obj ) { | |
return false; | |
} | |
$page = array_map( 'strval', (array) $page ); | |
if ( in_array( (string) $page_obj->ID, $page, true ) ) { | |
return true; | |
} elseif ( in_array( $page_obj->post_title, $page, true ) ) { | |
return true; | |
} elseif ( in_array( $page_obj->post_name, $page, true ) ) { | |
return true; | |
} else { | |
foreach ( $page as $pagepath ) { | |
if ( ! strpos( $pagepath, '/' ) ) { | |
continue; | |
} | |
$pagepath_obj = get_page_by_path( $pagepath ); | |
if ( $pagepath_obj && ( $pagepath_obj->ID == $page_obj->ID ) ) { | |
return true; | |
} | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment