2015-06-20 116 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);

$編曲中有