如何在wordpress中显示通过前端添加的注释

时间:2017-08-29 04:56:23

标签: php jquery ajax wordpress

我有一个博客部分,其中包含用于提交评论的表单。表单代码为:

<ul class="comment-list listnone">
    <li class="comment">
        <div class="comment-body mb40">
            <div class="comment-author"><img src="<?php echo content_url().'/themes/medilife/images/user-pic-2.jpg'?>" alt="" class="img-responsive img-circle"> </div>
            <div class="comment-info">
                <div class="comment-header"><h3 class="user-title">Name</h3></div>
                <div class="comment-content">
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
                    <div class="reply"><a href="#" class="btn btn-default btn-sm">Reply</a></div>
                </div>
            </div>
        </div>
    </li>
</ul>

我使用ajax调用将新添加的注释附加到现有注释中,ajax调用是:

jQuery(document).ready(function(){
    jQuery("#reply-form").submit(function(e) {
        e.preventDefault();
        var name = jQuery('#name').val();
        var email = jQuery('#email').val();
        var comment = jQuery('#textarea').val();
        jQuery.ajax({
            type:'POST',
            url:"<?=site_url()?>/ajaxcomment.php",
            data:{name:name, email:email, comment:comment},
            success:function(html){
                jQuery('.comment-list').append(html);
            }
        });
    });
});  

和ajax文件是:

<?php
    include 'wp-load.php';
    $id = array(
        'post_title'    =>  $_POST['name'],
        'post_type '        => 'comment'
    );
    // var_dump($userdata);
    $user_id = wp_insert_post( $id ) ;
    // print_r($user_id);
    $comment= $_POST['comment'];
    $email=$_POST['email'];

    add_post_meta($user_id,'comment',$comment);
    add_post_meta($user_id,'email',$email);
?>
<ul class="comment-list listnone">
    <li class="comment">
        <div class="comment-body mb40">
            <div class="comment-author"><img src="<?php echo content_url().'/themes/medilife/images/user-pic-1.jpg'?>" alt="" class="img-responsive img-circle"> </div>
            <div class="comment-info">
                <div class="comment-header"><h3 class="user-title"><?php echo $_POST['name'];?></h3></div>
                <div class="comment-content">
                    <p class="cmnt"><?php echo $_POST['comment'];?></p>
                    <div class="reply"><a href="#" class="btn btn-default btn-sm">Reply</a></div>
                </div>
            </div>
        </div>
    </li>
</ul>

因为在ajax调用之后我没有在我的html中使get方法动态,所以附加的数据会丢失。无论如何从后端获取评论并使其在htm部分动态化。

我创建了一个名为comments的自定义帖子类型,但由于评论是通过前端添加的,因此它们不会在wordpress后端显示为帖子

0 个答案:

没有答案