Wordpress:评论:提交评论会打开相关帖子

时间:2011-03-28 18:14:54

标签: php wordpress forms comments

我的评论表格有问题。

提交评论会导致打开相关帖子。

这是我的评论.php:

<?php
    if ( post_password_required() ) :
        echo '<h3 class="comments-header">' . __('Password Protected', 'buddypress') . '</h3>';
        echo '<p class="alert password-protected">' . __('Enter the password to view comments.', 'buddypress') . '</p>';
        return;
    endif;

    if ( is_page() && !have_comments() && !comments_open() && !pings_open() )
        return;
?>

<?php if ( have_comments() ) : ?>

    <div id="comments">

        <?php
            // Only include comments
            $numTrackBacks = 0; $numComments = 0;
            foreach ( (array)$comments as $comment )
                if ( 'comment' != get_comment_type() )
                    $numTrackBacks++;
                else
                    $numComments++;
        ?>

        <h3 id="comments">
            <?php
                printf( _n( '1 Kommentar', '%1$s Kommentare', $numComments, 'buddypress' ),
                number_format_i18n( $numComments ), '<em>' . get_the_title() . '</em>' );
            ?>
        </h3>

        <?php do_action( 'bp_before_blog_comment_list' ) ?>

        <ol class="commentlist">
            <?php wp_list_comments() ; ?>
        </ol><!-- .comment-list -->

        <?php do_action( 'bp_after_blog_comment_list' ) ?>

        <?php if ( get_option( 'page_comments' ) ) : ?>

            <div class="comment-navigation paged-navigation">

                <?php paginate_comments_links(); ?>

            </div>

        <?php endif; ?>

    </div><!-- #comments -->

<?php else : ?>

    <?php if ( pings_open() && !comments_open() && is_single() ) : ?>

        <p class="comments-closed pings-open">
            <?php printf( __('Comments are closed, but <a href="%1$s" title="Trackback URL for this post">trackbacks</a> and pingbacks are open.', 'buddypress'), trackback_url( '0' ) ); ?>
        </p>

    <?php elseif ( !comments_open() && is_single() ) : ?>

        <p class="comments-closed">
            <?php _e('Keine Kommentare erlaubt.', 'buddypress'); ?>
        </p>

    <?php endif; ?>

<?php endif; ?>

    <?php if ( comments_open() ) : ?>

    <div id="respond">

        <div class="comment-avatar-box">
            <div class="avb">
                <?php if ( bp_loggedin_user_id() ) : ?>
                    <a href="<?php echo bp_loggedin_user_domain() ?>">
                        <?php echo get_avatar( bp_loggedin_user_id(), 50 ); ?>
                    </a>
                <?php else : ?>
                    <?php echo get_avatar( 0, 50 ); ?>
                <?php endif; ?>
            </div>
        </div>

        <div class="comment-content">

            <h3 id="reply" class="comments-header">
                <?php comment_form_title( __( 'Leave a Reply', 'buddypress' ), __( 'Leave a Reply to %s', 'buddypress' ), true ); ?>
            </h3>

            <p id="cancel-comment-reply">
                <?php cancel_comment_reply_link( __( 'Click here to cancel reply.', 'buddypress' ) ); ?>
            </p>

            <?php if ( get_option( 'comment_registration' ) && !$user_ID ) : ?>

                <p class="alert">
                    <?php printf( __('You must be <a href="%1$s" title="Log in">logged in</a> to post a comment.', 'buddypress'), wp_login_url( get_permalink() ) ); ?>
                </p>

            <?php else : ?>

                <?php do_action( 'bp_before_blog_comment_form' ) ?>

                <form action="<?php echo site_url( 'wp-comments-post.php' ) ?>" method="post" id="commentform" class="standard-form">

                    <?php if ( $user_ID ) : ?>

                        <p class="log-in-out">
                            <?php printf( __('Logged in as <a href="%1$s" title="%2$s">%2$s</a>.', 'buddypress'), bp_loggedin_user_domain(), $user_identity ); ?> <a href="<?php echo wp_logout_url( get_permalink() ); ?>" title="<?php _e('Log out of this account', 'buddypress'); ?>"><?php _e('Log out &rarr;', 'buddypress'); ?></a>
                        </p>

                    <?php else : ?>

                        <?php $req = get_option( 'require_name_email' ); ?>

                        <p class="form-author">
                            <label for="author"><?php _e('Name', 'buddypress'); ?> <?php if ( $req ) : ?><span class="required"><?php _e('*', 'buddypress'); ?></span><?php endif; ?></label>
                            <input type="text" class="text-input" name="author" id="author" value="<?php echo $comment_author; ?>" size="40" tabindex="1" />
                        </p>

                        <p class="form-email">
                            <label for="email"><?php _e('Email', 'buddypress'); ?>  <?php if ( $req ) : ?><span class="required"><?php _e('*', 'buddypress'); ?></span><?php endif; ?></label>
                            <input type="text" class="text-input" name="email" id="email" value="<?php echo $comment_author_email; ?>" size="40" tabindex="2" />
                        </p>

                        <p class="form-url">
                            <label for="url"><?php _e('Website', 'buddypress'); ?></label>
                            <input type="text" class="text-input" name="url" id="url" value="<?php echo $comment_author_url; ?>" size="40" tabindex="3" />
                        </p>

                    <?php endif; ?>

                    <p class="form-textarea">
                        <label for="comment"><?php _e('Comment', 'buddypress'); ?></label>
                        <textarea name="comment" id="comment" cols="60" rows="10" tabindex="4"></textarea>
                    </p>

                    <?php do_action( 'bp_blog_comment_form' ) ?>

                    <p class="form-submit">
                        <input class="submit-comment button" name="submit" type="submit" id="submit" tabindex="5" value="<?php _e('Submit', 'buddypress'); ?>" />
                        <?php comment_id_fields(); ?>
                    </p>

                    <div class="comment-action">
                        <?php do_action( 'comment_form', $post->ID ); ?>
                    </div>

                </form>

                <?php do_action( 'bp_after_blog_comment_form' ) ?>

            <?php endif; ?>

        </div><!-- .comment-content -->
    </div><!-- #respond -->

    <?php endif; ?>

    <?php if ( $numTrackBacks ) : ?>
        <div id="trackbacks">

            <span class="title"><?php the_title() ?></span>

            <?php if ( 1 == $numTrackBacks ) : ?>
                <h3><?php printf( __( '%d Trackback', 'buddypress' ), $numTrackBacks ) ?></h3>
            <?php else : ?>
                <h3><?php printf( __( '%d Trackbacks', 'buddypress' ), $numTrackBacks ) ?></h3>
            <?php endif; ?>

            <ul id="trackbacklist">
                <?php foreach ( (array)$comments as $comment ) : ?>

                    <?php if ( get_comment_type() != 'comment' ) : ?>
                        <li><h5><?php comment_author_link() ?></h5><em>on <?php comment_date() ?></em></li>
                    <?php endif; ?>
                <?php endforeach; ?>
            </ul>
        </div>
    <?php endif; ?>

答案将不胜感激

更新

我的single.php:

<?php get_header(); ?>
<!-- LAESST DEN HEADER ERSCHEINEN -->

    <div id="main">

    <!-- DAS IST DER LOOP! HIER WIRD DER BEFEHL GEGEBEN, DEN INHALT ANZUZEIGEN (ARTIKEL ETC) -->

        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
           <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>   <!-- DIESER BEFEHL ZEIGT UND VERLINKT DIE ARTIKEL UEBERSCHRIFT -->
           <div class="entry">
               <?php the_content(); ?>
           </div>
        <?php endwhile; ?>

   <div class="author-box">

                        <p><?php echo get_avatar( get_the_author_meta( 'user_email' ), '50' ); ?></p>
                        <p><?php printf( __( 'Geschrieben von %s', 'buddypress' ), bp_core_get_userlink( $post->post_author ) ) ?>  am <?php the_time('j. F Y'); ?> um <?php the_time(); ?></p>
                        <p> Kategorie(n): <?php the_category(', '); ?> </p>
                    </div>

 <div id="related-posts"
    <?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
    $tag_ids = array();
    foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;

    $args=array(
        'tag__in' => $tag_ids,
        'post__not_in' => array($post->ID),
        'showposts'=>3, // Number of related posts that will be shown.
        'caller_get_posts'=>1
    );
    $my_query = new wp_query($args);
    if( $my_query->have_posts() ) {
        echo '<h3>Das k&ouml;nnte dich auch interessieren</h3><ul>';
        while ($my_query->have_posts()) {
            $my_query->the_post();
        ?>
            <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
        <?php
        }
        echo '</ul>';
    }
}
?>
</div>

<!--"NEXT POST" BZW "PREVIOUS POST" NAVIGATION -->

         <p align="center"><?php next_posts_link('&laquo; &Auml;ltere Eintr&auml;ge') ?>  <?php previous_posts_link('Neuere Eintr&auml;ge &raquo;') ?></p>

    <?php endif; ?>

    <?php comments_template(); ?> <!-- LAESST DAS KOMMENTARFELD ERSCHEINEN -->

    </div><!-- main -->

    <div id="sidebar">

        <?php get_sidebar(); ?>
        <!-- LAESST DIE SIDEBAR ERSCHEINEN -->

    </div><!-- sidebar -->

<?php get_footer(); ?>
<!-- DIESER BEFEHL LAESST DEN FOOTER ERSCHEINEN -->

但为什么显示不正确?

1 个答案:

答案 0 :(得分:0)

据我所见,代码中唯一可能导致您的问题的点是:

 <div class="comment-action">
      <?php do_action( 'comment_form', $post->ID ); ?>
</div>

$post->ID应该保留当前帖子的id或为null。如果$post出现问题,它将在调用此评论模板的页面上,可能是single.php。你可以尝试更改二十一个当前的single.php文件,看看是否能解决问题。或者在这里发布single.php文件内容,以便我们检查它。

修改

在single.php文件中,您正在执行新查询以获取相关帖子:

$args=array(
        'tag__in' => $tag_ids,
        'post__not_in' => array($post->ID),
        'showposts'=>3, // Number of related posts that will be shown.
        'caller_get_posts'=>1
    );
    $my_query = new wp_query($args);
if( $my_query->have_posts() ) {

此代码使用找到的每个相关帖子填充$post变量,因此一旦完成该代码,您将拥有$post var,其中包含使用该查询检索的相关帖子的最后一个帖子

最简单的解决方案:剪切相关帖子功能的代码(de“related-posts”div中的所有内容,包括开始和结束div标签)并将其粘贴到</div><!-- main -->之前,这样就不会影响评论或导航部分。