将PHP函数放在模板中可以工作,但将连接函数放在functions.php中则不行


putting php function in template works but putting concatenated function in functions.php doesn't?

下面是我正在使用的两个函数(第1行和第33行):http://pastebin.com/GWCJGS1i

如果您注意到第123行左右,我包含了连接函数. fb_comment_count() .。由于某种原因,这会产生不正确的注释计数。不管有多少注释,它只显示0。

但是,如果我将<?php echo 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(); ?>

两种可能的原因:

  1. 全局$post变量不是在你从functions.php调用函数时设置的,而是在模板中设置。
  2. get_posts()的调用将丢弃$post全局变量。