【问题标题】:WP Comments Not Posting CorrectlyWP 评论未正确发布
【发布时间】:2014-05-13 23:31:47
【问题描述】:

我一直在从头开始开发自定义 wordpress 主题,但在我的 cmets.php 文件中遇到了一个小问题。

https://gist.github.com/phillipdews/dcebfec2016a93bd0169

我认为问题源于该文件的第 44 行,因为当我尝试在我的博客文章上发表评论时,无论是否登录,评论过程都通过以下方式处理:

www.mydomain.com/postlink/wp-comments-post.php

当它自然需要去时:

www.mydomain.com/wp-comments-post.php

更新

这就是我决定要做的!我从头开始重新编写了 cmets.php 文件,如下所示:

<div id="comments">
    <?php if ( post_password_required() ) : ?>
    <p>This post is password protected. Enter the password to view and comments</p>
</div>
<?php 
        return;
    endif;
?>
<?php if ( have_comments() ) : ?>
<ol>
    <?php wp_list_comments ( array( 'callback' => 'BRUM_Theme_comment') ); ?>
</ol>
<?php
    elseif ( ! comments_open() && ! is_page() && post_type_supports( get_post_type(), 'comments' ) ) :
?>
<p>Comments are closed</p>
<?php endif; ?>
<?php comment_form(); ?>
</div>

然后我将这段 sn-p 代码添加到我的 functions.php 文件中!到目前为止,这让我的 cmets 出现了,人们也可以在我的博客文章中留下 cmets!但“回复”按钮尚未呈现。

<?php
function BRUM_Theme_comment( $comment, $args, $depth ){
    $GLOBALS['comment'] = $comment;
    ?>
    <?php if ( $comment->comment_approved == '1'): ?>
    <li>
        <article id="comment-<?php comment_ID() ?>">
            <?php echo get_avatar( $comment ); ?>
            <h4>
                <?php comment_author_link() ?>
            </h4>
            <time><a href="#comment-<?php comment_ID() ?>" pubdate><?php comment_date() ?> at <?php comment_time() ?></a></time>
            <?php comment_text() ?>
        </article>
        <?php endif;
}

到此为止!一旦我得到回复按钮,我将修改代码!

【问题讨论】:

  • 嗨 Phillip,如果您找到了问题的解决方案,请随时将您的解决方案作为答案发布,然后接受它。

标签: php wordpress wordpress-theming


【解决方案1】:

试试这个:

<div id="respond">

<?php

    if (!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
        die ('Please do not load this page directly. Thanks!');

    if ( post_password_required() ) { ?>
        This post is password protected. Enter the password to view comments.
    <?php
        return;
    }
?>

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

    <h2 id="comments"><?php comments_number('No comments', 'One comment', '% Comments' );?></h2>

    <div class="navigation">
        <div class="next-posts"><?php previous_comments_link() ?></div>
        <div class="prev-posts"><?php next_comments_link() ?></div>
    </div>

    <ol class="commentlist">
        <?php wp_list_comments(); ?>
    </ol>

    <div class="navigation">
        <div class="next-posts"><?php previous_comments_link() ?></div>
        <div class="prev-posts"><?php next_comments_link() ?></div>
    </div>

 <?php else : // this is displayed if there are no comments so far ?>

    <?php if ( comments_open() ) : ?>
        <!-- If comments are open, but there are no comments. -->

     <?php else : // comments are closed ?>
        <p>Comments are closed.</p>

    <?php endif; ?>

<?php endif; ?>

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



    <h2><?php comment_form_title( 'Leave an reply', 'Leave a reply in %s' ); ?></h2>
    <div class="cancel-comment-reply">
        <?php cancel_comment_reply_link(); ?>
    </div>

    <?php if ( get_option('comment_registration') && !is_user_logged_in() ) : ?>
        <p>You must be <a href="<?php echo wp_login_url( get_permalink() ); ?>">logged in</a> to post a comment.</p>
    <?php else : ?>
<br class="c" />

    <form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">

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

            <p>Logged as <a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php echo $user_identity; ?></a>. <a href="<?php echo wp_logout_url(get_permalink()); ?>" title="Log out of this account">Log out &raquo;</a></p>

        <?php else : ?>


            <div>
                <input type="text" value="Name" onblur="if (this.value == '') {this.value = 'Name';}"  onfocus="if (this.value == 'Name')   {this.value = '';}" name="author" id="author" size="22" tabindex="1" <?php if ($req) echo "aria-required='true'"; ?> />
      </div>

            <div>
                <input type="text" value="Email" onblur="if (this.value == '') {this.value = 'Email';}"  onfocus="if (this.value == 'Email')   {this.value = '';}" name="email" id="email" size="22" tabindex="2" <?php if ($req) echo "aria-required='true'"; ?> />
            </div>

            <div>
                <input type="text" value="Website" onblur="if (this.value == '') {this.value = 'Website';}"  onfocus="if (this.value == 'Website')   {this.value = '';}" name="url" id="url" size="22" tabindex="3" />
            </div>  

        <?php endif; ?>

<br />
        <div>
            <textarea name="comment" id="comment" cols="58" rows="10" tabindex="4"></textarea>
        </div>
<br class="c" />
        <div>

        <input name="submit" type="submit" id="submit" tabindex="5" value="Send" />
            <?php comment_id_fields(); ?>
      </div>

        <?php do_action('comment_form', $post->ID); ?>

    </form>

    <?php endif; // If registration required and not logged in ?>

</div>

<?php endif; ?>

这可能会解决您在 3.1 之后的任何 Wordpress 版本中的问题。 不要忘记将此代码放在您的模板文件夹(cmets.php)中

【讨论】:

    【解决方案2】:

    卢卡斯哥们,这完成了工作!我不得不稍微调整代码并从 OL 中删除类并调整我的 css,因为它使化身 100% 大而模糊!所以这就是我对我的 css 代码所做的一切,以使 cmets 的样式更好:

    #respond {float: left;}
    #respond img {width: auto; height: auto; float: left; margin-right: 4px; border: 1px solid #aab59a; padding: 1px; border-radius: 10px;}
    #respond ol li { background: #f1f1f1; margin:10px 0;padding:8px;border:2px solid #ccc;font-style:normal;list-style: none; border-radius: 10px;}
    

    这也是一个很好的功能,人们可以在他们的functions.php文件中使用它,因为它使回复链接到回复评论者姓名:

    /*
     * Change the comment reply link to use 'Reply to &lt;Author First Name>'
     */
    function add_comment_author_to_reply_link($link, $args, $comment){
    
        $comment = get_comment( $comment );
    
        // If no comment author is blank, use 'Anonymous'
        if ( empty($comment->comment_author) ) {
            if (!empty($comment->user_id)){
                $user=get_userdata($comment->user_id);
                $author=$user->user_login;
            } else {
                $author = __('Anonymous');
            }
        } else {
            $author = $comment->comment_author;
        }
    
        // If the user provided more than a first name, use only first name
        if(strpos($author, ' ')){
            $author = substr($author, 0, strpos($author, ' '));
        }
    
        // Replace Reply Link with "Reply to &lt;Author First Name>"
        $reply_link_text = $args['reply_text'];
        $link = str_replace($reply_link_text, 'Reply to ' . $author, $link);
    
        return $link;
    }
    
    add_filter('comment_reply_link', 'add_comment_author_to_reply_link', 10, 3);
    

    希望其他人觉得它有用,再次感谢它运行良好!

    【讨论】:

    • 太好了,很高兴解决了菲利普的问题!随意修改或更改任何您想要的方式。感谢分享你的函数代码!
    • 一切顺利!考虑一下,我可以离开 ol 类,然后将其添加到我的 css #respond > .commentlist ol li 现在只需为我的自定义主题执行搜索表单,然后返回 Wordpress Codex 和 github!
    猜你喜欢
    • 2017-02-06
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 2016-10-24
    相关资源
    最近更新 更多