我需要检查一个页面是否是页面的子页面(带有ID)。
我试图让这段代码正常工作,但没有成功。
function is_child($pageID) {
global $post;
echo $post->post_parent; // display the right ID!
if( is_page() && $post->post_parent == $pageID ) {
return true;
} else {
return false;
}
}
当$post->post_parent 返回正确的ID!测试代码(当$post->$post\\u父项在函数中回显正确的ID时,它始终返回no):
if(is_child(2310)) {
echo \'yes\';
} else {
echo \'no\';
}
此函数发生在我的函数中。php文件,其目的是通过条件语句加载CSS(如果页面是X或X的子级,则加载特定的CSS文件)。该代码可以在附近的许多网站上找到,但是在2012-2013年生成的。
非常感谢您的帮助。
未加工代码的来源:https://bavotasan.com/2011/is_child-conditional-function-for-wordpress/https://www.kevinleary.net/wordpress-is_child-for-advanced-navigation/