单后视图的输出缓冲区问题

时间:2012-07-30 作者:Zach

我创建了以下函数,用于加载单篇文章视图:

// Post
function basey_single_post() {

    global $post;
    ob_start();

    echo apply_filters(\'basey_page_title_news\', __(\'<h1>News</h1>\',\'basey\')); ?>
    <?php basey_post_before(); ?>
    <article <?php post_class() ?> id="post-<?php the_ID(); ?>">
        <?php basey_post_inside_before(); ?>
            <header>
                <?php echo apply_filters(\'basey_post_title\', \'<h2>\'.get_the_title().\'</h2>\'); ?>
                <?php basey_entry_meta($post->ID); ?>
                <div class="taxonomy">
                    <?php echo __(\'Posted in \',\'basey\'); the_category(\', \'); ?>
                </div>
                <?php $tag = get_the_tags(); if (!$tag) { } else { ?><div class="tags"><?php the_tags(); ?></div><?php } ?>
                <div class="commentLinks"><?php comments_popup_link( __( \' 0 Comments\', \'blank\' ), __( \' 1 Comment\', \'blank\' ), __( \' % Comments\', \'blank\' ), \'comments-link\', __(\'Comments closed\', \'blank\')); ?> <?php if ( comments_open() ) : ?>| <a href="<?php the_permalink(); ?>#respond" title="<?php echo __(\'Add a Comment\',\'basey\'); ?>"><?php echo __(\'Add a Comment\',\'basey\'); ?></a><?php endif; ?>
                </div>
            </header>
            <div class="entry-content">
                <?php the_content(); ?>
            </div>
            <footer>
                <?php wp_link_pages(array(\'before\' => \'<nav class="pagination"><p>\' . __(\'Pages:\', \'basey\'), \'after\' => \'</p></nav>\' )); ?>
            </footer>
        <?php basey_post_inside_after(); ?>
        <?php comments_template(); ?>
    </article>
    <?php basey_post_after();

    $display = apply_filters(\'basey_single_post_view\',ob_get_clean());
    return $display;
}
然后在需要时覆盖它,如:

// Post
function basey_single_post_override() {

    global $post;
    ob_start();

    echo \'TEST ADDITION\';

    echo apply_filters(\'basey_page_title_news\', __(\'<h1>News</h1>\',\'basey\')); ?>
    <?php basey_post_before(); ?>
    <article <?php post_class() ?> id="post-<?php the_ID(); ?>">
        <?php basey_post_inside_before(); ?>
            <header>
                <?php echo apply_filters(\'basey_post_title\', \'<h2>\'.get_the_title().\'</h2>\'); ?>
                <?php basey_entry_meta($post->ID); ?>
                <div class="taxonomy">
                    <?php echo __(\'Posted in \',\'basey\'); the_category(\', \'); ?>
                </div>
                <?php $tag = get_the_tags(); if (!$tag) { } else { ?><div class="tags"><?php the_tags(); ?></div><?php } ?>
                <div class="commentLinks"><?php comments_popup_link( __( \' 0 Comments\', \'blank\' ), __( \' 1 Comment\', \'blank\' ), __( \' % Comments\', \'blank\' ), \'comments-link\', __(\'Comments closed\', \'blank\')); ?> <?php if ( comments_open() ) : ?>| <a href="<?php the_permalink(); ?>#respond" title="<?php echo __(\'Add a Comment\',\'basey\'); ?>"><?php echo __(\'Add a Comment\',\'basey\'); ?></a><?php endif; ?>
                </div>
            </header>
            <div class="entry-content">
                <?php the_content(); ?>
            </div>
            <footer>
                <?php wp_link_pages(array(\'before\' => \'<nav class="pagination"><p>\' . __(\'Pages:\', \'basey\'), \'after\' => \'</p></nav>\' )); ?>
            </footer>
        <?php basey_post_inside_after(); ?>
        <?php comments_template(); ?>
    </article>
    <?php basey_post_after();

    $display = ob_get_clean();
    return $display;
}
add_filter(\'basey_single_post_view\',\'basey_single_post_override\');
获取此错误:Fatal error: Cannot redeclare basey_comment() (previously declared in /home/usr/public_html/dev/wp-content/themes/basey/comments.php:1) in /home/usr/public_html/dev/wp-content/themes/basey/comments.php on line 26 看起来像:

<?php function basey_comment($comment, $args, $depth) {
    $GLOBALS[\'comment\'] = $comment; ?>
    <li <?php comment_class(); ?>>
        <article id="comment-<?php comment_ID(); ?>">
            <header class="comment-author vcard">
                <?php echo get_avatar($comment,$size=\'32\'); ?>
        <?php printf(__(\'<cite class="fn">%s</cite>\', \'basey\'), get_comment_author_link()); ?>
        <time datetime="<?php echo comment_date(\'c\'); ?>"><a href="<?php echo htmlspecialchars(get_comment_link($comment->comment_ID)); ?>"><?php printf(__(\'%1$s\', \'basey\'), get_comment_date(),  get_comment_time()); ?></a></time>
        <?php edit_comment_link(__(\'(Edit)\', \'basey\'), \'\', \'\'); ?>
            </header>

      <?php if ($comment->comment_approved == \'0\') { ?>
        <div class="alert alert-block fade in">
          <a class="close" data-dismiss="alert">&times;</a>
          <p><?php _e(\'Your comment is awaiting moderation.\', \'basey\'); ?></p>
        </div>
      <?php } ?>

            <section class="comment">
                <?php comment_text() ?>
            </section>

      <?php comment_reply_link(array_merge($args, array(\'depth\' => $depth, \'max_depth\' => $args[\'max_depth\']))); ?>

        </article>
<?php } ?>

<?php if (post_password_required()) { ?>
    <section id="comments">
    <div class="alert alert-block fade in">
      <a class="close" data-dismiss="alert">&times;</a>
    <p><?php _e(\'This post is password protected. Enter the password to view comments.\', \'basey\'); ?></p>
    </div>
  </section><!-- /#comments -->
    <?php
        return;
} ?>

<?php if (have_comments()) { ?>
    <section id="comments">
    <h3><?php printf(_n(\'One Response to &ldquo;%2$s&rdquo;\', \'%1$s Responses to &ldquo;%2$s&rdquo;\', get_comments_number(), \'basey\'), number_format_i18n(get_comments_number()), get_the_title()); ?></h3>

        <ol class="commentlist">
      <?php wp_list_comments(array(\'callback\' => \'basey_comment\')); ?>
        </ol>

    <?php if (get_comment_pages_count() > 1 && get_option(\'page_comments\')) { // are there comments to navigate through ?>
      <nav id="comments-nav" class="pager">
        <div class="previous"><?php previous_comments_link(__(\'&larr; Older comments\', \'basey\')); ?></div>
        <div class="next"><?php next_comments_link(__(\'Newer comments &rarr;\', \'basey\')); ?></div>
            </nav>

    <?php } // check for comment navigation ?>

    <?php if (!comments_open() && !is_page() && post_type_supports(get_post_type(), \'comments\')) { ?>
      <p><?php _e(\'Comments are closed.\', \'basey\'); ?></p>
    <?php } ?>
  </section><!-- /#comments -->
<?php } ?>

<?php if (!have_comments() && !comments_open() && !is_page() && post_type_supports(get_post_type(), \'comments\')) { ?>
    <section id="comments">
    <p><?php _e(\'Comments are closed.\', \'basey\'); ?></p>
  </section><!-- /#comments -->
<?php } ?>

<?php if (comments_open()) { ?>
<section id="respond">
  <h3><?php comment_form_title( __(\'Leave a Reply\', \'basey\'), __(\'Leave a Reply to %s\', \'basey\') ); ?></h3>
    <p class="cancel-comment-reply"><?php cancel_comment_reply_link(); ?></p>
    <?php if (get_option(\'comment_registration\') && !is_user_logged_in()) { ?>
  <p><?php printf( __(\'You must be <a href="%s">logged in</a> to post a comment.\', \'basey\'), wp_login_url( get_permalink() ) ); ?></p>
    <?php } else { ?>
    <form action="<?php echo get_option(\'siteurl\'); ?>/wp-comments-post.php" method="post" id="commentform">
        <?php if (is_user_logged_in()) { ?>
    <p><?php printf(__(\'Logged in as <a href="%s/wp-admin/profile.php">%s</a>.\', \'basey\'), get_option(\'siteurl\'), $user_identity); ?> <a href="<?php echo wp_logout_url(get_permalink()); ?>" title="<?php __(\'Log out of this account\', \'basey\'); ?>"><?php _e(\'Log out &raquo;\', \'basey\'); ?></a></p>
        <?php } else { ?>
      <label for="author"><?php _e(\'Name\', \'basey\'); if ($req) _e(\' (required)\', \'basey\'); ?></label>
            <input type="text" class="text" name="author" id="author" value="<?php echo esc_attr($comment_author); ?>" size="22" tabindex="1" <?php if ($req) echo "aria-required=\'true\'"; ?>>
      <label for="email"><?php _e(\'Email (will not be published)\', \'basey\'); if ($req) _e(\' (required)\', \'basey\'); ?></label>
            <input type="email" class="text" name="email" id="email" value="<?php echo esc_attr($comment_author_email); ?>" size="22" tabindex="2" <?php if ($req) echo "aria-required=\'true\'"; ?>>
      <label for="url"><?php _e(\'Website\', \'basey\'); ?></label>
            <input type="url" class="text" name="url" id="url" value="<?php echo esc_attr($comment_author_url); ?>" size="22" tabindex="3">
        <?php } ?>
      <label for="comment"><?php _e(\'Comment\', \'basey\'); ?></label>
        <textarea name="comment" id="comment" class="input-xlarge" tabindex="4"></textarea>
        <input name="submit" class="btn btn-primary" type="submit" id="submit" tabindex="5" value="<?php _e(\'Submit Comment\', \'basey\'); ?>">
        <?php comment_id_fields(); ?>
        <?php do_action(\'comment_form\', $post->ID); ?>
    </form>
    <?php } // if registration required and not logged in ?>
  </section><!-- /#respond -->
<?php }
如果能在这方面提供任何帮助,我将不胜感激。谢谢

编辑:这可能更多的是关于如何有效地正确使用输出缓冲区和/或WP过滤器的问题。谢谢

1 个回复
最合适的回答,由SO网友:Matthew Boynes 整理而成

很可能comments.php 多次包含或需要文件。您可以确保对该文件的任何调用都是通过include_oncerequire_once 为了防止这个问题。

否则,安全的解决方法是使用以下内容包装文件内容:

if ( ! function_exists( \'basey_comment\' ) ) :
   # code goes here
endif;
只有在函数尚未声明的情况下,才会“处理”文件。

结束