2015-06-20 115 views
3

我在wordpress中只创建一个网站(index.php)。页面的每个部分循环并从某个帖子中引入内容(基于其ID)。其中一部分还有一个评论框以及发布内容以及发布的评论。但是,我遇到的问题是,一旦发布了评论(点击发送按钮),就会加载single.php。这个想法是,网站上没有固定链接,只显示了帖子的内容,因此只能将用户保留在索引页上。我需要添加什么样的代码才能发布评论不会加载single.php,因此会重新加载index.php?单页WordPress网站与评论框

谢谢。

编辑: 只给我使用的代码示例: 上的index.php我使用:

<?php $category = array('category_name' => 'My category'); ?> 
<?php query_posts($category); ?> 
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
    <div class="articleWrap"> 
     <?php the_content(); ?> 
    </div> 
    <?php endwhile; endif; ?> 
<?php wp_reset_query(); ?> 

,并在部分我要评论框:

<?php $other_category = array('category_name' => 'My other category'); ?> 
<?php query_posts($other_category); ?> 
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
    <div class="articleWrap"> 
     <?php the_content(); ?> 
     <?php $withcomments = 1; comments_template(); ?> 
    </div> 
    <?php endwhile; endif; ?> 
<?php wp_reset_query(); ?> 

我的评论模板(comments.php了),我在调用的代码是:

<div class="messageBox"> 
<?php 
$comments_args = array(
    'comment_notes_after' => '', 
    'label_submit'=>'Submit', 
    'title_reply' => '', 
    'logged_in_as' => '', 
    'comment_field' => '<p class="comment-form-comment"><label for="comment"></label><br /><textarea id="comment" name="comment" aria-required="true"></textarea></p>', 
); 
comment_form($comments_args); 
?> 
</div> <!-- //messageBox --> 
<div class="commentBoxesWrap"> 
    <?php wp_list_comments('type=comment&callback=showcomments'); //this is a call back to a function in functions.php ?> 
</div> <!-- //commentBoxesWrap --> 
+0

使用AJAX您的意见?提供关于你的代码的更多细节,如何发布评论? – sinhayash

+0

那个评论框是你写的一个插件吗? –

+0

sinhayash是对的:你需要使用jquery/ajax(http://api.jquery.com/jquery.post/)。是否有人点击提交按钮,ajax会调用single.php(?)来保存文章并发回内容或格式化的html - 这个信息可以用jquery append(http://api.jquery .com/append /) – WebDevel

回答

0

使用jQuery和AJAX进行onLoad,从PHP函数中获取所有评论数据(推测名称,日期和评论?)。您可能会使用GET请求,但如果您想要使用日期过滤器,则可以使用POST请求并将过滤器作为参数传递。你正在有效地制作一个API。

的jQuery:

$.ajax({ 
 
\t \t \t  type: "POST", 
 
\t \t \t  url: "http://example.com/comment-load.php", 
 
\t \t \t  data: { 
 
\t \t \t   date: filter_date, 
 
\t \t \t   tag: filter_tag 
 
\t \t \t  }, 
 
\t \t \t  dataType: "json" 
 
\t \t \t }) 
 
\t \t \t  .done(function (comment) { 
 
\t \t \t  var i = 0; 
 
\t \t \t  var leg = $(comment).length; 
 
\t \t \t  while (i < leg) { 
 
\t \t \t   $("#comments").append("<div>" + comment[i] + "</div>"); 
 
\t \t \t   i = i + 1; 
 
\t \t \t  } 
 
    });

而且PHP:

回声json_encode($ ARR);

$编曲中有