简体   繁体   English

如何在Wordpress中调用儿童主题

[英]How To Call A Child Theme in Wordpress

I am currently trying to edit someone elses code and they have written the following; 我目前正在尝试编辑其他人的代码,他们编写了以下内容;

?>

<?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>';
}
?>

However, this code returns the following error for is_subpage 但是,此代码为is_subpage返回以下错误

( ! ) Fatal error: Call to undefined function is_subpage() in aaaaaaaaa.php on line 53 (is_sub)

I believe someome was trying to check if the parent theme has a child theme? 我相信有人正在尝试检查父主题是否有子主题? Does anyone know the proper way to acheive this? 有谁知道实现这一目标的正确方法?

Thanks in advance! 提前致谢!

Wordpress does not have a core function, that checks if you are currently on a sub/childpage - So at some point, a function must have been lost - WordPress没有核心功能,该功能可以检查您当前是否在子页面/子页面上-因此在某些时候,该功能肯定已经丢失-

It looks very much like the function that I use myself, taken from github : Here 它看起来非常像我自己使用的功能,取自github: 这里

If at some point, this github page is removed - I will paste the function here as well. 如果在某个时候,此github页面已删除-我也将在此处粘贴函数。

With this function - everything should work again. 使用此功能-一切都应该再次工作。


Function : 功能说明

/**
* 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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM