把php函数放在模板中但是在函数中放入连接函数.php不行?

时间:2011-07-19 20:22:33

标签: php wordpress

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

如果您注意第123行,我已经包含了连接函数. fb_comment_count() .。由于某种原因,这会产生错误的评论计数。无论有多少评论,它都只显示零。

但是,如果我将<?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 个答案:

答案 0 :(得分:0)

两个可能的原因:

  1. 当您从functions.php调用函数时,未设置全局$ post变量,但是在模板内部时。
  2. 调用get_posts()会破坏$ post global。