【架構篇】WordPress佈景主題製作【comments.php】

【架構篇】WordPress佈景主題製作【comments.php】

本篇要來介紹製作WordPress佈景主題之一的 comments.php 檔案,可用於當作頁面、文章留言使用,也能夠搭配JetPack外掛加強留言功能。

在製作 comments.php 之前需要在 funciton.php 內加上下列函式,主要是為了秀出作者、get_avatar 圖片,以及秀出文章留言格式,幫函顯示時間、日期、編輯、留言作者、留言內容。

<?php //留言區塊
function displaycomments($comment, $args, $depth){
$GLOBALS['comment'] = $comment; ?>
<?php echo get_avatar( $comment, 80 ); ?>
<?php printf(('<span class="fn">%s</span>'), get_comment_author_link()) ?>
<?php printf(('%1$s @ %2$s'), get_comment_date(),  get_comment_time()) ?> <?php edit_comment_link() //__('%1$s @ %2$s') ?>
<?php if ($comment->comment_approved == '0') : ?>
你的留言正在審核中。
<?php endif;?>
<?php comment_text(); ?>
<?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
<?php }?>

而在 comments.php 所放上的是判斷是否在這篇文章上有留言,以及留言編輯區塊。

<?php if ( post_password_required() ) return;?>
<?php comments_number('沒有留言', '留言 [ 1 ]', '留言 [ % ]' );?>
<?php if ( have_comments() ) : ?>
<?php wp_list_comments(); ?>
<?php paginate_comments_links();?>
<?php else : ?>
<?php if ( comments_open() ) : ?>
<p> 本篇文章目前沒有留言。 </p>
<?php else : ?>
<p> 本篇文章目前不開放留言。 </p>
<?php endif; ?>
<?php endif; ?>
<?php
$comments_args = array(
        'label_submit'=>'送出',
        'title_reply'=>'留言',
        'comment_notes_after' => '',
        'comment_field' =>'<p class="comment-form-comment"><textarea id="comment" name="comment" aria-required="true"></textarea></p>',
);
comment_form($comments_args);
?>

最後如果像是在 single.php 頁面需要使用時,只需要放上 comments_template() 語法即可。

<?php get_header(); ?>
<?php the_title(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php comments_template(); ?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

關於WordPress系列文章事項
文章版本:
第 2.0 版本,WordPress 系列教學純屬個人經驗,若有錯誤會重新修正並加上版本號碼,目前會持續調整樣式、整理佈景語法,目前版本已調整為先教學基本架構再教學設計樣式,之後會以最終版本告知。

相關文章
作者簡介
個人頭像照片
努力寫文中!