【问题标题】:WordPress: issues trying to get the comments to show up on the main pageWordPress:试图让评论显示在主页上的问题
【发布时间】:2012-09-06 23:35:12
【问题描述】:

我正在尝试让 cmets 显示在主页上,但我遇到了问题。

$withcomments = 1;
comments_template();

我已将上面的 PHP 代码放在我的 index.php 文件中。出现注释框,但由于某些原因,cmets 没有出现。显然,这段代码是为了让 cmets 和评论框都出现,所以我对为什么 cmets 没有出现感到有点困惑。有人有解决办法吗?

【问题讨论】:

标签: php wordpress indexing comments


【解决方案1】:

你可以试试wp_list_comments()这个函数:

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

法典中的更多信息:http://codex.wordpress.org/Function_Reference/wp_list_comments

【讨论】:

    【解决方案2】:

    如果不在单个帖子或页面上,Wordpress 不会显示 cmets 模板。 如果您想展示,那么您必须创建满足您要求的自定义功能。

    当前函数位于 wp-includes >> comment-template.php 文件第 851 行。

    这里是显示cmets列表的自定义函数,放在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 ); ?> 
    

    希望这篇文章对你有所帮助。一切顺利;)

    【讨论】:

      猜你喜欢
      • 2011-09-06
      • 2011-10-21
      • 1970-01-01
      • 1970-01-01
      • 2011-03-12
      • 1970-01-01
      • 1970-01-01
      • 2014-09-06
      • 1970-01-01
      相关资源
      最近更新 更多