Wordpress: Comments: Submitting a comment opens a

2019-08-18 15:42发布

I have a problem with my comments form.

Submitting a comment leads to open a related post.

Here's my 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; ?>

An answer would be appreciated

Update:

My 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 -->

But why is it displayed incorrectly?

1条回答
Deceive 欺骗
2楼-- · 2019-08-18 16:37

The only point in your code that could be responsible for your problem, as far as I can see, is at:

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

$post->ID should hold the id of the current post or be null. If there is a problem with $post, it will be on the page that calls this comments template, single.php probably. You could try changing your current single.php file for the twentyten one and see if that solves the problem. Or post here that single.php file contents so we can check it.

EDIT:

On the single.php file, you have a new query being executed to get the related posts:

$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() ) {

This code is populating the $post variable with each of the related posts found, so once that code is done you will have the $post var holding the last post of the related posts retrieved with that query.

Easiest solution: cut the code for the related posts functionality (everything inside de "related-posts" div, including the opening and closing div tags) and paste it just before the </div><!-- main --> so it doesn't affect the comments or navigation parts.

查看更多
登录 后发表回答