《最新wordpress主題制作之新手教程系列(十)》要點(diǎn):
本文介紹了最新wordpress主題制作之新手教程系列(十),希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
導(dǎo)讀:制作comments php今天我們來制作評(píng)論主題的評(píng)論模塊.在主題目錄Aurelius下新建comments php,在single php剪切以下代碼,粘貼到comments p
制作comments.php
DEDECMS案例今天我們來制作評(píng)論主題的評(píng)論模塊.在主題目錄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();??>
DEDECMS案例?????以上語句的作用就是將comments.php里的所有內(nèi)容導(dǎo)入到single.php中,與直接在single.php寫comments.php中的代碼效果是一樣的.
DEDECMS案例?????為了平安起見,不讓惡意用戶直接打開評(píng)論文件,請(qǐng)?jiān)赾omments.php頭部添加以下代碼:
DEDECMS案例?????因?yàn)閃ordPress的輸出評(píng)論函數(shù)wp_list_comments()輸出的評(píng)論代碼與我們主題的評(píng)論代碼不一樣的,我們得自定義我們的評(píng)論列表,將comments.php中的以下代碼刪除(以下代碼用于列出文章的所有評(píng)論):
代碼如下:
<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 ligul? a 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”>請(qǐng)輸入暗碼再查看評(píng)論內(nèi)容.</a></p>
??? </li>
??? <?php
??????? } else if ( !comments_open() ) {
??? ?>
??? <li class=”decmt-box”>
??????? <p><a href=”#addcomment”>評(píng)論功能已經(jīng)關(guān)閉!</a></p>
??? </li>
??? <?php
??????? } else if ( !have_comments() ) {
??? ?>
??? <li class=”decmt-box”>
??????? <p><a href=”#addcomment”>還沒有任何評(píng)論,你來說兩句吧</a></p>
??? </li>
??? <?php
??????? } else {
??????????? wp_list_comments(‘type=comment&callback=aurelius_comment’);
??????? }
??? ?>
? 以上代碼的意思大致也可以看得出來了,就是一大堆?如果…就….,如果以上條件都不滿足就列出所有評(píng)論.現(xiàn)在將主題文件夾Aurelius中的functions.php中的 ?> ,改成以下代碼,如果你之前從本博客下載到的functions.php已經(jīng)有以下代碼則不用再添加:?
代碼如下:
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’ => ‘回復(fù)’,‘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”>發(fā)表于:<?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>你的評(píng)論正在審核,稍后會(huì)顯示出來!</em><br />
??????? <?php endif; ?>
??????? <?php comment_text(); ?>
??????????? </div>
??????? </div>
<?php } ?>
以上代碼所用到的WordPress函數(shù)及相應(yīng)的說明:
函數(shù)名稱 |
函數(shù)功能 |
get_avatar($comment, 48) |
獲取評(píng)論者的gravatar頭像,尺寸為48 * 48 |
comment_reply_link() |
回復(fù)留言的鏈接 |
get_comment_author_link |
用于獲取評(píng)論者博客地址 |
get_comment_time |
獲取評(píng)論發(fā)布時(shí)間 |
edit_comment_link |
管理員修改評(píng)論的鏈接 |
comment_text() |
輸出評(píng)論內(nèi)容 |
DEDECMS案例?????好,現(xiàn)在在你的文章頁(yè)面底部就可以正常地顯示評(píng)論了!現(xiàn)在我們繼續(xù)來制作提交評(píng)論的表單,將以下代碼刪除(也就是評(píng)論表單的代碼):
?
?
代碼如下:
<!– 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> 才能頒發(fā)評(píng)論.</p>
<?php else? : ?>
<!– Comment Form –>
<form id=”commentform” name=”commentform” action=”<?php echo get_option(‘siteurl’); ?>/wp-comments-post.php” method=”post”>
??? <h3>頒發(fā)評(píng)論</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”>網(wǎng)址(選填)</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”>評(píng)論內(nèi)容</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=”document.forms['commentform'].submit()” class=”button medium black right”>頒發(fā)評(píng)論</a> </li>
??? </ul>
??? <?php comment_id_fields(); ?>
??? <?php do_action(‘comment_form’, $post->ID); ?>
</form>
<?php endif; ?>
函數(shù)名稱 |
函數(shù)功能 |
is_user_logged_in |
判斷用戶是否登錄 |
wp_login_url |
博客登錄地址 |
get_comment_author_link |
用于獲取評(píng)論者博客地址 |
$comment_author |
讀取cookie,如果該用戶之前已經(jīng)頒發(fā)過評(píng)論則自動(dòng)幫助用戶填寫用戶名 |
$comment_author_email |
讀取cookie,如果該用戶之前已經(jīng)頒發(fā)過評(píng)論則自動(dòng)幫助用戶填寫Email |
$comment_author_url |
讀取cookie,如果該用戶之前已經(jīng)頒發(fā)過評(píng)論則自動(dòng)幫助用戶填寫博客地址 |
do_action(‘comment_form’, $post->ID); |
該函數(shù)為某些插件預(yù)留 |
wp_logout_url |
退出登錄的鏈接 |
DEDECMS案例好了,評(píng)論模板comments.php制作完畢!
歡迎參與《最新wordpress主題制作之新手教程系列(十)》討論,分享您的想法,維易PHP學(xué)院為您提供專業(yè)教程。
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/13795.html