Php 如何在Wordpress中调用子主题
我目前正试图编辑别人的代码,他们编写了以下代码:Php 如何在Wordpress中调用子主题,php,wordpress,Php,Wordpress,我目前正试图编辑别人的代码,他们编写了以下代码: ?> <?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->
?>
<?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_子页面
(!)致命错误:在aaaaaaa.php的第53行(is_sub)调用未定义的函数is_subpage()
我相信有人试图检查父主题是否有子主题?有人知道实现这一点的正确方法吗
提前谢谢 Wordpress没有一个核心功能,它检查您当前是否在子/子页面上-因此在某个时候,一个功能一定丢失了- 它看起来非常像我自己使用的函数,取自github: 如果在某个时候,这个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;
}
}
/**
*是子页面吗?
*
*检查当前页是否为子页,并返回true或false。
*
*@param$page混合可选(post_name或ID)进行检查。
*@param$single boolean optional-默认值true用于检查所有父级,false用于检查直接父级。
*@返回布尔值
*/
函数是子页面($page=null,$all=true){
全球$员额;
//这是一页吗?
如果(!is_page())
返回false;
//它有父母吗?
如果(!isset($post->post\u parent)或$post->post\u parent post\u parent==$page)
返回true;
}else if(是字符串($page)){
//获得祖先
$parents=get_祖先($post->ID,'page');
//它有祖先吗?
如果(空($parents))
返回false;
如果($all==true){//检查所有父项
//环游祖先
foreach($parents作为$parent){
$parent=get_post($parent);
如果(is_page()&&&$parent->post_name==$page){
返回true;
}
}
}else{//检查直接父项
//得到第一个祖先
$parent=get_post($parents[0]);
//比较post\u名称
如果($parent->post_name==$page)
返回true;
}
}
返回false;
}
}
Wordpress没有一个核心功能,用于检查您当前是否在子/子页面上-因此在某个时候,某个功能一定丢失了-
它看起来非常像我自己使用的函数,取自github:
如果在某个时候,这个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;
}
}
/**
*是子页面吗?
*
*检查当前页是否为子页,并返回true或false。
*
*@param$page混合可选(post_name或ID)进行检查。
*@param$single boolean optional-默认值true用于检查所有父级,false用于检查直接父级。
*@返回布尔值
*/
函数是子页面($page=null,$all=true){
全球$员额;
//这是一页吗?
如果(!is_page())
返回false;
//它有父母吗?
如果(!isset($post->post\u parent)或$post->post\u parent post\u parent==$page)
返回true;
}else if(是字符串($page)){
//获得祖先
$parents=get_祖先($post->ID,'page');
//它有祖先吗?
如果(空($parents))
返回false;
如果($all==true){//检查所有父项
//环游祖先
foreach($parents作为$parent){
$parent=get_post($parent);
如果(is_page()&&&$parent->post_name==$page){
返回true;
}
}
}else{//检查直接父项
//得到第一个祖先
$parent=get_post($parents[0]);
//比较post\u名称
如果($parent->post_name==$page)
返回true;
}
}
返回false;
}
}
此错误定义在同一文件中没有调用is_subpage()函数。因此,请调用函数中所需的函数和功能。啊,对不起,我想我没有遵循。你是说这个错误是说函数没有原点吗?是的,没错…:)is_subpage()不是一个标准的WP函数-可能来自这里的github-将它添加到functions.php文件中,它应该可以再次工作。非常感谢@Stender此错误定义在同一文件中没有调用is_subpage()函数。因此,请调用函数中所需的函数和功能。啊,对不起,我想我没有遵循。你是说这个错误是说函数没有原点吗?是的,没错…:)is_subpage()不是一个标准的WP函数-可能来自这里的github-将它添加到functions.php文件中,它应该可以再次工作。非常感谢@斯坦德