将php函数放在模板中有效,但将连接函数放在functions.php中无效';T

将php函数放在模板中有效,但将连接函数放在functions.php中无效';T,php,wordpress,Php,Wordpress,以下是我正在使用的两个函数(第1行和第33行): 如果您注意到第123行,我已经包含了连接函数。fb_comment_count()。。由于某些原因,这会产生不正确的注释计数。不管有多少评论,它都显示为零 但是,如果我在页面模板中插入,它就可以正常工作。为什么会发生这种情况?如何获得正确的注释计数以显示串联函数 以下是页面模板: <?php get_header();?> <section id="content">

以下是我正在使用的两个函数(第1行和第33行):

如果您注意到第123行,我已经包含了连接函数
。fb_comment_count()。
。由于某些原因,这会产生不正确的注释计数。不管有多少评论,它都显示为零

但是,如果我在页面模板中插入
,它就可以正常工作。为什么会发生这种情况?如何获得正确的注释计数以显示串联函数

以下是页面模板:

            <?php get_header();?>

            <section id="content">

                <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

                <article class="post" id="post-<?php the_ID(); ?>">
                    <h2><?php the_title(); ?></h2>
                        <section class="entry">
                            <?php echo fb_comment_count(); ?>
                            <p class="attachment"><?php echo wp_get_attachment_image( $post->ID, 'medium' ); ?></p>
                            <div class="caption">
                                <?php if ( !empty($post->post_excerpt) ) the_excerpt(); // this is the "caption" ?>
                            </div>
                        </section>
                </article>

            <?php comments_template(); ?>

            <?php endwhile; else: ?>

                <p>Sorry, no attachments matched your criteria.</p>

            <?php endif; ?>

            </section>

            <?php get_sidebar(); ?>
            <?php get_footer(); ?>

两个可能的原因:

  • 全局$post变量在从functions.php调用函数时不设置,但在模板内部时设置
  • 对get_posts()的调用破坏了$post global

  • 我发布了页面模板。这有助于缩小问题的范围吗?您是否在fb_comment_count()函数中进行了echo或其他调试以检查$post的值?你的模板代码片段证明你的函数在“循环”中,所以$post将出现,但不知道你何时调用你的add_过滤器(“gallery”)代码。不,你能解释一下我是如何做到的吗?我还是一个业余爱好者。在你的fb\u comment\u count()函数中,你正在使用$post->ID,我怀疑这个var在一个实例中没有初始化,所以你可以在$postrl=get\u permalink($post->ID)后面添加一行;要将其回显以检查其是否具有预期值,请执行以下操作:echo“id=”$post->IDI这样做了,它生成了这个:
    id=818id=818id=818id=818id=818id=818id=818id=818id=818id=818