我可以有两个单独的.php文件,其中一个只显示帖子,另一个显示特定帖子的评论吗?

时间:2013-07-10 作者:Sosthenes Kwame Boame

显然,我知道我不能有两个单人间。php文件。有注释的那个必须有不同的名称。。。也许是一条评论。php之类的。

我在一家餐厅评论网站上工作,由于页面空间不足,我决定在页面一侧显示一些最新评论(客户评论)的摘录,然后在另一个页面上创建指向所有其他评论(客户评论)和评论表单的链接。我曾想过灯箱,但我认为它并不适合我想要实现的目标。

关于如何完成这项工作,有什么建议吗?谢谢

EDIT:我为评论创建了一个页面模板,并将其命名为comments side。php。代码如下:

<?php
/*
 * Template Name: Comments Page
 */
 global $data; //get theme options

?>

<div id="comments-template">

    <div class="comments-wrap">

        <div id="comments">

            <?php if ( have_comments() ) : ?>

                <h3 id="comments-number" class="comments-header"><?php comments_number( __( \'No Comments\', \'supreme\' ), __( \'One Comment\', \'supreme\' ), __( \'% Comments\', \'supreme\' ) ); ?></h3>

                <?php do_atomic( \'before_comment_list\' );// supreme_before_comment_list ?>

                <?php if ( get_option( \'page_comments\' ) ) : ?>
                    <div class="comment-navigation comment-pagination">
                        <span class="page-numbers"><?php printf( __( \'Page %1$s of %2$s\', \'supreme\' ), ( get_query_var( \'cpage\' ) ? absint( get_query_var( \'cpage\' ) ) : 1 ), get_comment_pages_count() ); ?></span>
                        <?php paginate_comments_links(); ?>
                    </div><!-- .comment-navigation -->
                <?php endif; ?>

                <ol class="comment-list">
                    <?php wp_list_comments( hybrid_list_comments_args() ); ?>
                </ol><!-- .comment-list -->

                <?php do_atomic( \'after_comment_list\' ); // supreme_after_comment_list ?>

            <?php endif; ?>

            <?php if ( pings_open() && !comments_open() ) : ?>

                <p class="comments-closed pings-open">
                    <?php printf( __( \'Comments are closed, but <a href="%1$s" title="Trackback URL for this post">trackbacks</a> and pingbacks are open.\', \'supreme\' ), get_trackback_url() ); ?>
                </p><!-- .comments-closed .pings-open -->

            <?php elseif ( !comments_open() ) : ?>

                <p class="comments-closed">
                    <?php _e( \'Comments are closed.\', \'supreme\' ); ?>
                </p><!-- .comments-closed -->

            <?php endif; ?>

        </div><!-- #comments -->

        <?php $comment_args = array( \'fields\' => apply_filters( \'comment_form_default_fields\', array(
                        \'author\' => \'<div class="form_row clearfix">\' .
                                    \'<input id="author" name="author" type="text" value="\' .
                                    esc_attr( $commenter[\'comment_author\'] ) . \'" size="30"\' . @$aria_req . \' PLACEHOLDER="\'.__(\'Your name\',\'supreme\').\'"/>\' .
                                    ( $req ? \' <span class="required">*</span>\' : \'\' ) .
                                    \'</div><!-- #form-section-author .form-section -->\',
                        \'email\'  => \'<div class="form_row clearfix">\' .
                                    \'<input id="email" name="email" type="text" value="\' . esc_attr(  $commenter[\'comment_author_email\'] ) . \'" size="30"\' . @$aria_req . \' PLACEHOLDER="\'.__(\'Email Address\',\'supreme\').\'"/>\' .
                                    ( $req ? \' <span class="required">*</span>\' : \'\' ) .
                            \'</div><!-- #form-section-email .form-section -->\',
                        \'url\'    => \'<div class="form_row clearfix">\' .
                                    \'<input id="url" name="url" type="text" value="\' . esc_attr(  $commenter[\'comment_author_url\'] ) . \'" size="30"\' . @$aria_url . \' PLACEHOLDER="\'.__(\'Website\',\'supreme\').\'"/>\'.\'</div>\')),
                        \'comment_field\' => \'<div class="form_row clearfix">\' .
                                    \'<textarea id="comments" name="comment" cols="45" rows="8" aria-required="true" PLACEHOLDER="\'.__(\'Comments\',\'supreme\').\'"></textarea>\' .
                                    ( $req ? \' <span class="required">*</span>\' : \'\' ) .
                                    \'</div><!-- #form-section-comment .form-section -->\',
                        \'comment_notes_after\' => \'\',
                        \'title_reply\' => __( \'Add a comment\', \'supreme\' ),
                    );
                    if(get_option(\'default_comment_status\') ==\'open\'){
                        comment_form($comment_args); } // Loads the comment form.  ?>

    </div><!-- .comments-wrap -->

</div><!-- #comments-template -->
我在Wordpress的管理中创建了一个页面,并为该页面选择了评论页面模板。这就是我的函数。处理注释的php:

function mytheme_comment($comment, $args, $depth) {
        $GLOBALS[\'comment\'] = $comment;
        extract($args, EXTR_SKIP);

        if ( \'div\' == $args[\'style\'] ) {
            $tag = \'div\';
            $add_below = \'comment\';
        } else {
            $tag = \'li\';
            $add_below = \'div-comment\';
        }
?>
        <<?php echo $tag ?> <?php comment_class(empty( $args[\'has_children\'] ) ? \'\' : \'parent\') ?> id="comment-<?php comment_ID() ?>">
        <?php if ( \'div\' != $args[\'style\'] ) : ?>
        <div id="div-comment-<?php comment_ID() ?>" class="comment-body">
        <?php endif; ?>
        <div class="comment-author vcard">
        <?php if ($args[\'avatar_size\'] != 0) echo get_avatar( $comment, $args[\'avatar_size\'] ); ?>
        <?php printf(__(\'<cite class="fn">%s</cite> <span class="says">says:</span>\'), get_comment_author_link()) ?>
        </div>
<?php if ($comment->comment_approved == \'0\') : ?>
        <em class="comment-awaiting-moderation"><?php _e(\'Your comment is awaiting moderation.\') ?></em>
        <br />
<?php endif; ?>

        <div class="comment-meta commentmetadata"><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>">
            <?php
                /* translators: 1: date, 2: time */
                printf( __(\'%1$s at %2$s\'), get_comment_date(),  get_comment_time()) ?></a><?php edit_comment_link(__(\'(Edit)\'),\'  \',\'\' );
            ?>
        </div>

        <?php comment_text($comment_ID); ?> 

        <a href="comments-side/#comment-<?php comment_ID() ?>" class="comment-more">read more</a>

        <div class="reply">
        <?php comment_reply_link(array_merge( $args, array(\'add_below\' => $add_below, \'depth\' => $depth, \'max_depth\' => $args[\'max_depth\']))) ?>
        </div>
        <?php if ( \'div\' != $args[\'style\'] ) : ?>
        </div>
        <?php endif; ?>
<?php
        }
这就是我在《单间餐厅》一角发表评论的方式。php

<div class="sidereviews clearfix"> 
<h3>Recent Reviews</h3>  
<?php comments_template( \'/comments-side.php\', true ); // Loads the comments-side.php template. ?>
</div><!--/sidereviews-->
还有另一个评论文件(comments.php)处理博客上的评论。我应该改变或摆脱什么?我做错了什么?谢谢

2 个回复
最合适的回答,由SO网友:Vigs 整理而成

创建一个页面模板来显示特定“评论”即帖子的评论。

然后,您可以链接到该页面,并在url中包含帖子ID和$\\u从新页面的url中获取帖子ide,然后查询帖子并显示其评论。

这应该至少为您指明了正确的方向。最终模板Kwame以以下内容结束:

<?php
    /*
    Template Name: Handle Review Comments
    */
    get_header(); ?>

    <?php

    echo \'<div id="content" class="barsclubs-single">\';
    if(isset($_GET[\'location\'])){
          $location = $_GET[\'location\'];
          $postIdVariable = $location;
          $post = get_post( $postIdVariable );    
    echo \'You are viewing reviews for \'.\'<a href="\'.get_post_permalink().\'">\'.$post->post_title.\'</a>\';
          $location = $_GET[\'location\'];
          $defaults = array(
             \'post_id\' => $location,
             \'number\' => 10,
              );
    $comments = get_comments($defaults);
    wp_list_comments( $args, $comments );
    comment_form( $args, $location );
    }

    echo \'</div>\';
    include_once( \'footer.php\' );
    ?>
使用模板创建页面。然后像这样向它发送变量。

www.yoursite.com/yourpagename/?test=postid

$url=\'www.yoursite.com/yourpagename/?test=\'.$post->ID;
echo \'<a href="\'.$url.\' title="View Review Comments" target="_BLANK"\';
将当前帖子的id发送到脚本。我相信必须在循环中使用。

Edit:

您可以在此处看到它的作用:

脚本正在提取id为963的帖子的评论,这是我在url中发送的id。

http://oq.publicvent.org/handle-review-comments/?test=963

Edit 2: Trying to make this simple for you.

创建一个文件,并根据需要命名。将此代码放入其中:

    <?php
    /*
    Template Name: Handle Review Comments
    */
    include_once( \'header.php\' );
    if(isset($_GET[\'test\'])){
    echo \'you passed this script the post id: \'.$_GET[\'test\'];
    $test = $_GET[\'test\'];
    $defaults = array(
        \'post_id\' => $test,
        \'number\' => 10,
             );
    $comments = get_comments($defaults);
    wp_list_comments( $args, $comments ); 
    comment_form( $args, $test );
    }
    include_once( \'footer.php\' );
    ?>
将其保存并上载到服务器。创建一个新页面并将其命名为“所有评论”,使用页面模板的(处理评论)模板,从右侧的下拉列表中选择它。

现在请发帖。php或单个。php或无论您如何显示您的评论,您都可以使用它来显示该评论的5条最新评论。包括在底部链接到其他评论。

<div class="sidereviews clearfix"> 
<h3>Recent Reviews</h3>  
<?php 
    $defaults = array(
        \'post_id\' => $post->ID,
        \'number\' => 5, //get 5 most recent comments
             );
    $comments = get_comments($defaults);
    wp_list_comments( $args, $comments );
?>
<?php echo \'<h3><a href="\'.site_url().\'/all-reviews/?test=\'.$post->ID.\'">All Reviews</a></h3>\'; ?>
</div><!--sidereviews-->

EDIT 3: POST TITLE

Kwame代码:

 $post = get_post( $postIdVariable );    
 echo \'You are viewing reviews for \'.$post->post_title;
 $location = $_GET[\'location\'];
$postdvariable本质上是一个空变量,因为$\\u GET[\'location\'];尚未运行。

将上面的代码段更改为:

$location = $_GET[\'location\'];
$postIdVariable = $location;    
$post = get_post( $postIdVariable );    
echo \'You are viewing reviews for \'.\'<a href="\'.get_post_permalink().\'">\'.$post->post_title.\'</a>\';

SO网友:Vigs

我和家人一起去度周末。以下是使用评论的其中一个页面的链接:http://ghananights.villagepixels.com/bars-clubs/the-republic-bar-and-grill/ (还需要一些造型呵呵)。。。永久链接位置错误。在日期之间设置了链接,实际上没有任何线索,与“在底部查看所有评论…如何显示评论的摘录,而不是完整的评论,然后添加链接到实际评论的“阅读更多”链接不同?此外,我尝试使用->post\\u title,它返回该页面的标题(位置评论),而不是帖子(Republic Bar and Grill)…谢谢-Kwame Boame

试图使用自定义comment callback 解决上述问题:

替换:

wp_list_comments( $args, $comments );
使用:

$defaults = array(
    \'post_id\' => $post->ID,
    \'number\' => 5, //get 5 most recent comments
);

$comments = get_comments($defaults);

$commentsArgs = array(
    \'callback\'   => \'kwame_comments\',
    \'reply_text\' => null,
);

wp_list_comments( $commentsArgs, $comments );
将此放置在functions.php:

function kwame_comments($comment, $args, $depth) {
    $GLOBALS[\'comment\'] = $comment;
    extract($args, EXTR_SKIP);

    if ( \'div\' == $args[\'style\'] ) {
        $tag = \'div\';
        $add_below = \'comment\';
    } else {
        $tag = \'li\';
        $add_below = \'div-comment\';
    }
?>
    <<?php echo $tag ?> <?php comment_class(empty( $args[\'has_children\'] ) ? \'\' : \'parent\') ?> id="comment-<?php comment_ID() ?>">

    <?php if ( \'div\' != $args[\'style\'] ) : ?>
        <div id="div-comment-<?php comment_ID() ?>" class="comment-body">
    <?php endif; ?>

    <div class="comment-author vcard">
        <?php printf(__(\'<cite class="fn">%s</cite> <span class="says">says:</span>\'), get_comment_author_link()) ?>
    </div>

    <?php if ($comment->comment_approved == \'0\') : ?>
        <em class="comment-awaiting-moderation"><?php _e(\'Your comment is awaiting moderation.\') ?></em>
        <br />
    <?php endif; ?>

    <div class="comment-meta commentmetadata">
        <a href="<?php echo htmlspecialchars( site_url().\'/location-review/?location=\'.$comment->comment_post_ID.\'#comment-\'.$comment->comment_ID ); ?>" title="View Review">
        <?php
        /* translators: 1: date, 2: time */
        printf( __(\'%1$s at %2$s\'), get_comment_date(),  get_comment_time()) ?></a><?php edit_comment_link(__(\'(Edit)\'),\'  \',\'\' );
        ?>
    </div>

    <?php
    $comment_contents = get_comment_text();

    //if comment contents is longer than 50 characters
    if(strlen($comment_contents > 50)){
        echo substr($comment_contents, 0, 50);

        //echo only first 50 characters
        echo $comment_contents;
    } else {
        echo $comment_contents;
    }
?>

    <div class="reply">
        <a href="<?php echo htmlspecialchars( site_url().\'/location-review/?location=\'.$comment->comment_post_ID.\'&replytocom=\'.$comment->comment_ID.\'#respond\' ); ?>" title="View Review">Reply</a>
    </div>

    <?php if ( \'div\' != $args[\'style\'] ) : ?>
        </div>
    <?php endif; ?>

<?php
}
这在本地主机上对我有效。

结束