check if current page is a child of a specified top level page
I am writing some code that will embed a chat bot to specified pages on my site. I have a function/shortcode that accepts 2 parameters, include
and exclude
. The value of each can be a single post ID or a comma-separated string of post IDs (that are later converted to arrays in the shortcode function). If both parameters are null
(default), the bot will be embedded on every page of the site. If correct values are passed to either, the function will only add the bot to pages whose ID are in the include
array and/or will NOT add it to pages with ID in the exclude
array.
if (in_array($id, $exclude) || bene_get_pods_val('disable_chatbot') === true) return;
if ((count($include) === 0 count($exclude) === 0) || in_array($id, $include)) {
embed_bot();
}
On top of this I'd like to be able to exclude or include entire sections of pages (Products, Solutions, Company, Knowledge Base, etc) on the site. I know I can pass something to the shortcode telling it to include/exclude pages who are children of the 1 or more of the IDs passed. No problem there.
if( $include_children $top_level_page_id === wp_get_post_parent_id(get_the_ID()) ) {
return true;
}
My issue comes in with custom post types. For example, I have a Knowledge Base section that uses a regular post as its top-level page (i.e. the page located at www.example.com/knowledge-base/
) but all of the content pages within that section are of a custom post type. For that reason, I cannot make them children of that top-level page, and therefore each of their parent page IDs returns 0.
I know I can check $_SERVER['REQUEST_URI']
for a specific slug
function pageIsInSection($top_level_page_id) {
$is = false;
$parent_slug = get_post_field('post_name', $top_level_page_id);
if(false !== strpos($_SERVER['REQUEST_URI'], '/$parent_slug/')) {
$is = true;
}
return $is;
}
but that seems like a hack. Is there a better way to find out if the current page is a Knowledge Base (or any other section with custom post types) page?
Topic children custom-post-types Wordpress
Category Web