2012-09-13 74 views
0

我試圖讓評論顯示在主頁上,但我遇到了問題。WordPress的:試圖讓評論顯示在主頁上的問題

$withcomments = 1; 
comments_template(); 

我已經把PHP代碼放在了我的index.php文件中。評論框出現,但由於某些原因,評論沒有。顯然,這段代碼是爲了使評論和評論框都顯示出來,所以我有點困惑,爲什麼評論沒有顯示出來。有沒有人有任何解決方案?

+0

可能重複:// stackoverflow.com/questions/6205002/show-comments-on-wordpress-home-page) – iambriansreed

+0

您應該將techguy4web的答案標記爲已接受。 –

回答

0

如果不是在單個帖子或頁面上,Wordpress不會顯示評論模板。 如果你想顯示,那麼你必須創建您的自定義功能,這是滿足您的要求。

當前函數位於WP-包括>>發表評論-的template.php文件行號851

這是自定義函數來顯示評論列表,把它放在functions.php的主題文件:

 function custom_comments_template($file = '/comments.php', $separate_comments = false) { 
      global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_ID, $user_identity, $overridden_cpage; 


      if (empty($file)) 
       $file = '/comments.php'; 

      $req = get_option('require_name_email'); 

      /** 
      * Comment author information fetched from the comment cookies. 
      * 
      * @uses wp_get_current_commenter() 
      */ 
      $commenter = wp_get_current_commenter(); 

      /** 
      * The name of the current comment author escaped for use in attributes. 
      */ 
      $comment_author = $commenter['comment_author']; // Escaped by sanitize_comment_cookies() 

      /** 
      * The email address of the current comment author escaped for use in attributes. 
      */ 
      $comment_author_email = $commenter['comment_author_email']; // Escaped by sanitize_comment_cookies() 

      /** 
      * The url of the current comment author escaped for use in attributes. 
      */ 
      $comment_author_url = esc_url($commenter['comment_author_url']); 

      /** @todo Use API instead of SELECTs. */ 
      if ($user_ID) { 
       $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND (comment_approved = '1' OR (user_id = %d AND comment_approved = '0')) ORDER BY comment_date_gmt", $post->ID, $user_ID)); 
      } else if (empty($comment_author)) { 
       $comments = get_comments(array('post_id' => $post->ID, 'status' => 'approve', 'order' => 'ASC')); 
      } else { 
       $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND (comment_approved = '1' OR (comment_author = %s AND comment_author_email = %s AND comment_approved = '0')) ORDER BY comment_date_gmt", $post->ID, wp_specialchars_decode($comment_author,ENT_QUOTES), $comment_author_email)); 
      } 

      // keep $comments for legacy's sake 
      $wp_query->comments = apply_filters('comments_array', $comments, $post->ID); 
      $comments = &$wp_query->comments; 
      $wp_query->comment_count = count($wp_query->comments); 
      update_comment_cache($wp_query->comments); 

      if ($separate_comments) { 
       $wp_query->comments_by_type = &separate_comments($comments); 
       $comments_by_type = &$wp_query->comments_by_type; 
      } 

      $overridden_cpage = false; 
      if ('' == get_query_var('cpage') && get_option('page_comments')) { 
       set_query_var('cpage', 'newest' == get_option('default_comments_page') ? get_comment_pages_count() : 1); 
       $overridden_cpage = true; 
      } 

      if (!defined('COMMENTS_TEMPLATE') || !COMMENTS_TEMPLATE) 
       define('COMMENTS_TEMPLATE', true); 

      $include = apply_filters('comments_template', STYLESHEETPATH . $file); 
      if (file_exists($include)) 
       require($include); 
      elseif (file_exists(TEMPLATEPATH . $file)) 
       require(TEMPLATEPATH . $file); 
      else // Backward compat code will be removed in a future release 
       require(ABSPATH . WPINC . '/theme-compat/comments.php'); 
     } 

現在你可以在你的模板中使用此功能:

 <?php custom_comments_template('', true); ?> 

希望這個線程可以幫助你。所有最優秀的;)

+0

它工作!非常感謝! – user1636968

相關問題