我目前正在尝试编辑别人的代码,他们写了以下内容;
?>
<?php get_header(); ?>
<?php include(TEMPLATEPATH.'/navigation.php'); ?>
<div class="main">
<?php
if(is_subpage()){ /// THIS LINE RETURNS ERROR
$parent_title = get_the_title($post->post_parent);
echo '<h2>Distributor Support Site</h2>';
}
?>
但是,此代码会为is_subpage
( ! ) Fatal error: Call to undefined function is_subpage() in aaaaaaaaa.php on line 53 (is_sub)
我相信有人试图检查父主题是否有子主题?有没有人知道实现这个的正确方法?
提前致谢!
答案 0 :(得分:2)
Wordpress没有核心功能,可以检查您当前是否在子/子页面上 - 所以在某些时候,功能必定已经丢失 -
它看起来非常像我自己使用的函数,取自github:Here
如果在某个时候,这个github页面被删除了 - 我也会在这里粘贴这个函数。
使用此功能 - 一切都应该再次运作。
功能:
/**
* Is SubPage?
*
* Checks if the current page is a sub-page and returns true or false.
*
* @param $page mixed optional ( post_name or ID ) to check against.
* @param $single boolean optional - default true to check against all parents, false to check against immediate parent.
* @return boolean
*/
function is_subpage( $page = null, $all = true ) {
global $post;
// is this even a page?
if ( ! is_page() )
return false;
// does it have a parent?
if ( ! isset( $post->post_parent ) OR $post->post_parent <= 0 )
return false;
// is there something to check against?
if ( ! isset( $page ) ) {
// yup this is a sub-page
return true;
} else {
// if $page is an integer then its a simple check
if ( is_int( $page ) ) {
// check
if ( $post->post_parent == $page )
return true;
} else if ( is_string( $page ) ) {
// get ancestors
$parents = get_ancestors( $post->ID, 'page' );
// does it have ancestors?
if ( empty( $parents ) )
return false;
if ( $all == true ) { // check against all parents
// loop through ancestors
foreach ( $parents as $parent ) {
$parent = get_post( $parent );
if ( is_page() && $parent->post_name == $page) {
return true;
}
}
} else { // check against immediate parent
// get the first ancestor
$parent = get_post( $parents[0] );
// compare the post_name
if ( $parent->post_name == $page )
return true;
}
}
return false;
}
}