PHP WordPress 中的function_exists不适用于我的主题


function_exists in PHP WordPress is not working for my theme

我正在尝试开发一个WordPress主题。我正在使用在WordPress插件中声明的函数。我在主题中声明了另一个函数,当插件未激活时,我想使用我的主题函数。所以我写了以下代码:

<?php
    if (function_exists(shaplatools_post_thumbnail_gallery())) {
        shaplatools_post_thumbnail_gallery();
    } else {
        boishakhimela_post_thumbnail();
    }
?>

但是当插件未激活时,它会显示以下错误:

致命错误:调用第 11 行/var/www/wordpress/wp-content/themes/boishakhimela/content-page.php 中的未定义函数 shaplatools_post_thumbnail_gallery)

shaplatools_post_thumbnail_gallery()内容如下:

function shaplatools_post_thumbnail_gallery() {
    if ( post_password_required() || is_attachment() || ! has_post_thumbnail() ) {
        return;
    }
    if ( is_singular() ) :
        $image_gallery = get_post_meta( get_the_ID(), '_shaplatools_image_gallery', true );
        if( ! empty( $image_gallery ) ) :
            ?>
                <div class="post-thumbnail">
                    <?php
                        if( function_exists( 'shaplatools_image_gallery' ) ) {
                            echo shaplatools_image_gallery();
                        }
                    ?>
                </div>
            <?php
        else : 
            ?>
                <div class="post-thumbnail">
                    <?php 
                        if ( has_post_thumbnail() ) {
                            the_post_thumbnail();
                        } 
                    ?>
                </div><!-- .post-thumbnail -->
            <?php
        endif;
        ?>
    <?php else : ?>
        <a class="post-thumbnail" href="<?php the_permalink(); ?>" aria-hidden="true">
            <?php
                the_post_thumbnail( 'post-thumbnail', array( 'alt' => get_the_title() ) );
            ?>
        </a>
    <?php endif; // End is_singular()
}

那么我该如何解决这个问题呢?

你应该使用: function_exists("shaplatools_post_thumbnail_gallery")

您需要将函数名称作为字符串传递,请参阅 http://php.net/function_exists 哪些状态

参数

function_name 字符串形式的函数名称。

如果你使用 function_exists(shaplatools_post_thumbnail_gallery())那么你实际上是在调用函数shaplatools_post_thumbnail_gallery()并使用它的返回值作为function_exists()的参数