站長資訊網
最全最豐富的資訊網站

WordPress主題制作全過程(十):制作comments.php

前面給大家介紹了《WordPress主題制作全過程(九):制作single.php》,本文繼續給大家介紹如何制作comments.php,下面一起來看一下吧~

WordPress主題制作全過程(十):制作comments.php

php入門到就業線上教程:進入學習

今天我們來制作評論主題的評論模塊。在主題目錄Aurelius下新建comments.php,在single.php剪切以下代碼,粘貼到comments.php:

<!– Comment’s List –> <h3>Comments</h3> <div class="hr dotted clearfix"> </div> <ol class="commentlist"> <li class="comment"> <div class="gravatar"> <img alt="" src=’images/gravatar.png’ height=’48′ width=’48′ /> <a class="comment-reply-link" href=">Reply</a> </div> <div class="comment_content"> <div class="clearfix"> <cite class="author_name"><a href="">Joe Bloggs</a></cite> <div class="comment-meta commentmetadata">January 6, 2010 at 6:26 am</div> </div> <div class="comment_text"> <p>Donec leo. Aliquam risus elit, luctus vel, interdum vitae, malesuada eget, elit. Nulla vitae ipsum. Donec ligula ante, bibendum sit amet, elementum quis, viverra eu, ante. Fusce tincidunt. Mauris pellentesque, arcu eget feugiat accumsan, ipsum mi molestie orci, ut pulvinar sapien lorem nec dui.</p> </div> </div> </li> </ol> <div class="hr clearfix"> </div> <!– Comment Form –> <form id="comment_form" action="" method="post"> <h3>Add a comment</h3> <div class="hr dotted clearfix"> </div> <ul> <li class="clearfix"> <label for="name">Your Name</label> <input id="name" name="name" type="text" /> </li> <li class="clearfix"> <label for="email">Your Email</label> <input id="email" name="email" type="text" /> </li> <li class="clearfix"> <label for="email">Your Website</label> <input id="website" name="website" type="text" /> </li> <li class="clearfix"> <label for="message">Comment</label> <textarea id="message" name="message" rows="3" cols="40"></textarea> </li> <li class="clearfix"> <!– Add Comment Button –> <a type="submit" class="button medium black right">Add comment</a> </li> </ul> </form>
登錄后復制

在single.php原位置添加代碼:

<?php comments_template(); ?>
登錄后復制

以上語句的作用就是將comments.php里的所有內容導入到single.php中,與直接在single.php寫comments.php中的代碼效果是一樣的。

為了安全起見,不讓惡意用戶直接打開評論文件,請在comments.php頭部添加以下代碼:

<?php if (isset($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME'])) die ('Please do not load this page directly. Thanks!'); ?>
登錄后復制

因為WordPress的輸出評論函數wp_list_comments()輸出的評論代碼與我們主題的評論代碼不一樣的,我們得自定義我們的評論列表,將comments.php中的以下代碼刪除(以下代碼用于列出文章的所有評論):

<li class="comment"> <div class="gravatar"> <img alt="" src=’images/gravatar.png’ height=’48′ width=’48′ /> <a class="comment-reply-link" href=">Reply</a> </div> <div class="comment_content"> <div class="clearfix"> <cite class="author_name"><a href="">Joe Bloggs</a></cite> <div class="comment-meta commentmetadata">January 6, 2010 at 6:26 am</div> </div> <div class="comment_text"> <p>Donec leo. Aliquam risus elit, luctus vel, interdum vitae, malesuada eget, elit. Nulla vitae ipsum. Donec ligula ante, bibendum sit amet, elementum quis, viverra eu, ante. Fusce tincidunt. Mauris pellentesque, arcu eget feugiat accumsan, ipsum mi molestie orci, ut pulvinar sapien lorem nec dui.</p> </div> </div> </li>
登錄后復制

改成:

<?php      if (!empty($post->post_password) && $_COOKIE['wp-postpass_' . COOKIEHASH] != $post->post_password) {          // if there's a password         // and it doesn't match the cookie     ?>     <li class="decmt-box">         <p><a href="#addcomment">請輸入密碼再查看評論內容.</a></p>     </li>     <?php          } else if ( !comments_open() ) {     ?>     <li class="decmt-box">         <p><a href="#addcomment">評論功能已經關閉!</a></p>     </li>     <?php          } else if ( !have_comments() ) {      ?>     <li class="decmt-box">         <p><a href="#addcomment">還沒有任何評論,你來說兩句吧</a></p>     </li>     <?php          } else {             wp_list_comments('type=comment&callback=aurelius_comment');         }     ?>
登錄后復制

以上代碼的意思大致也可以看得出來了,就是一大堆 如果…就….,如果以上條件都不滿足就列出所有評論。現在將主題文件夾Aurelius中的functions.php中的 ?> ,改成以下代碼,如果你之前從本博客下載到的functions.php已經有以下代碼則不用再添加:

function aurelius_comment($comment, $args, $depth)  {    $GLOBALS['comment'] = $comment; ?>    <li class="comment" id="li-comment-<?php comment_ID(); ?>"> <div class="gravatar"> <?php if (function_exists('get_avatar') && get_option('show_avatars')) { echo get_avatar($comment, 48); } ?>  <?php comment_reply_link(array_merge( $args, array('reply_text' => '回復','depth' => $depth, 'max_depth' => $args['max_depth']))) ?> </div> <div class="comment_content" id="comment-<?php comment_ID(); ?>"> <div class="clearfix"> <?php printf(__('<cite class="author_name">%s</cite>'), get_comment_author_link()); ?> <div class="comment-meta commentmetadata">發表于:<?php echo get_comment_time('Y-m-d H:i'); ?></div>    <?php edit_comment_link('修改'); ?> </div> <div class="comment_text"> <?php if ($comment->comment_approved == '0') : ?> <em>你的評論正在審核,稍后會顯示出來!</em><br />       <?php endif; ?>       <?php comment_text(); ?> </div> </div> <?php } ?>
登錄后復制

以上代碼所用到的WordPress函數及相應的說明:

函數名稱 函數功能
get_avatar($comment, 48) 獲取評論者的gravatar頭像,尺寸為48 * 48
comment_reply_link() 回復留言的鏈接
get_comment_author_link 用于獲取評論者博客地址
get_comment_time 獲取評論發布時間
edit_comment_link 管理員修改評論的鏈接
comment_text() 輸出評論內容

好,現在在你的文章頁面底部就可以正常地顯示評論了!現在我們繼續來制作提交評論的表單,將以下代碼刪除(也就是評論表單的代碼):

<!– Comment Form –> <form id="comment_form" action="" method="post"> <h3>Add a comment</h3> <div class="hr dotted clearfix"> </div> <ul> <li class="clearfix"> <label for="name">Your Name</label> <input id="name" name="name" type="text" /> </li> <li class="clearfix"> <label for="email">Your Email</label> <input id="email" name="email" type="text" /> </li> <li class="clearfix"> <label for="email">Your Website</label> <input id="website" name="website" type="text" /> </li> <li class="clearfix"> <label for="message">Comment</label> <textarea id="message" name="message" rows="3" cols="40"></textarea> </li> <li class="clearfix"> <!– Add Comment Button –> <a type="submit" class="button medium black right">Add comment</a> </li> </ul> </form>
登錄后復制

改成:

<?php  if ( !comments_open() ) : // If registration required and not logged in. elseif ( get_option('comment_registration') && !is_user_logged_in() ) :  ?> <p>你必須 <a href="<?php echo wp_login_url( get_permalink() ); ?>">登錄</a> 才能發表評論.</p> <?php else  : ?> <!-- Comment Form --> <form id="commentform" name="commentform" action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post">     <h3>發表評論</h3>     <div class="hr dotted clearfix"> </div>     <ul>         <?php if ( !is_user_logged_in() ) : ?>         <li class="clearfix">             <label for="name">昵稱</label>             <input type="text" name="author" id="author" value="<?php echo $comment_author; ?>" size="23" tabindex="1" />         </li>         <li class="clearfix">             <label for="email">電子郵件</label>             <input type="text" name="email" id="email" value="<?php echo $comment_author_email; ?>" size="23" tabindex="2" />         </li>         <li class="clearfix">             <label for="email">網址(選填)</label>             <input type="text" name="url" id="url" value="<?php echo $comment_author_url; ?>" size="23" tabindex="3" />         </li>         <?php else : ?>         <li class="clearfix">您已登錄:<a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php echo $user_identity; ?></a>. <a href="<?php echo wp_logout_url(get_permalink()); ?>" title="退出登錄">退出 ?</a></li>         <?php endif; ?>         <li class="clearfix">             <label for="message">評論內容</label>             <textarea id="message comment" name="comment" tabindex="4" rows="3" cols="40"></textarea>         </li>         <li class="clearfix">             <!-- Add Comment Button -->             <a href="javascript:void(0);" onClick="Javascript:document.forms['commentform'].submit()" class="button medium black right">發表評論</a> </li>     </ul>     <?php comment_id_fields(); ?>     <?php do_action('comment_form', $post->ID); ?> </form> <?php endif; ?>
登錄后復制

函數名稱 函數功能
is_user_logged_in 判斷用戶是否登錄
wp_login_url 博客登錄地址
get_comment_author_link 用于獲取評論者博客地址
$comment_author 讀取cookie,如果該用戶之前已經發表過評論則自動幫助用戶填寫用戶名
$comment_author_email 讀取cookie,如果該用戶之前已經發表過評論則自動幫助用戶填寫Email
$comment_author_url 讀取cookie,如果該用戶之前已經發表過評論則自動幫助用戶填寫博客地址
do_action('comment_form', $post->ID); 該函數為某些插件預留
wp_logout_url 退出登錄的鏈接

推薦學習:《WordPress教程》

贊(0)
分享到: 更多 (0)
網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
国产精品美女久久久m| 精品综合一区二区三区| 91亚洲国产成人久久精品网址| 亚洲精品无码专区在线在线播放| 亚洲国产午夜中文字幕精品黄网站| 四虎成人精品无码永久在线| 精品真实国产乱文在线| 亚洲日韩精品无码AV海量| 91热成人精品国产免费| 99久久国产综合精品1尤物| 久久精品国产亚洲网站| 91麻豆精品国产| 国产福利电影一区二区三区久久久久成人精品综合 | 久久国产成人精品麻豆| 国产精品部在线观看| 六月婷婷精品视频在线观看| 日韩精品久久不卡中文字幕| 国产精品秘入口18禁麻豆免会员| 国产精品亚洲综合久久| 九九精品免费视频| 久久九九精品国产av片国产| 亚洲国产精品免费观看| 精品一区二区三区四区在线播放| 日韩精品真人荷官无码| 亚洲成人精品久久| 少妇人妻偷人精品无码视频| 久re这里只有精品最新地址| 久久精品隔壁老王影院| 久久国产乱子伦精品免费看| 久久九九精品国产综合喷水| 日韩精品亚洲人成在线观看| 久久精品毛片免费观看| 2021国内精品久久久久精免费| 精品无人码麻豆乱码1区2区| 蜜臀98精品国产免费观看| 国产精品久久久久久福利漫画| 2017国产精品自拍| 精品国产乱码欠欠欠欠精品| 精品久久洲久久久久护士免费| 国产精品JIZZ在线观看无码| 久久九九久精品国产日韩经典|