首页 文章

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

提问于
浏览
1

我正在尝试开发一个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()
}

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

1 回答

  • 0

    你应该使用: 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() 的参数

相关问题