我们准备使用一个名为“留言”页面来当作留言本,使用页面的评论实现留言功能,但是会有一个问题,就是页面里的留言名称都是“评论”。我们在这里主要解决这个问题,让留言本看起来更“像”。
这个例子里的主题使用 stardust,增加留言本后的主题下载。
目录
1. 增加留言本(Guestbook)模块
2. 增加留言本页面
3. 根据自己的实际需要修改模板
1. 增加留言本(Guestbook)模块
复制文件 wordpress/wp-content/themes/stardust/page.php 到 wordpress/wp-content/themes/stardust/guestbook.php。
使用文本编辑器编辑文件 wordpress/wp-content/themes/stardust/guestbook.php。
在
<?php get_header(); ?> |
前加入
<?php /* Template Name: Guestbook */ ?> |
结果如下
<?php /* Template Name: Guestbook */ ?> <?php get_header(); ?> |
找到
<?php comments_template(); // Get wp-comments.php template ?> |
修改为
<?php comments_template('/guestcomments.php'); // Get wp-comments.php template ?> |
编辑后的文件如下
<?php /* Template Name: Guestbook */ ?> <?php get_header(); ?> <div id="menu1"> <ul> <li class="page_item"><a href="<?php bloginfo('url'); ?>/" title="主页">主页</a></li> <?php wp_list_pages('title_li=0&depth=1'); ?> </ul> </div> </div><!-- end header --> <hr /> <div id="wrapper"> <div id="content"> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <div class="post" id="post-<?php the_ID(); ?>"> <h2 class="storytitle"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2> <div class="meta"><span class="user"><?php the_author() ?></span> <?php edit_post_link(__('Edit This')); ?></div> <div class="storycontent"> <?php the_content(__('(more...)')); ?> </div> </div> <?php comments_template('/guestcomments.php'); // Get wp-comments.php template ?> <?php endwhile; else: ?> <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> <?php endif; ?> <?php posts_nav_link(' — ', __('« Previous Page'), __('Next Page »')); ?> <?php get_footer(); ?> |
复制文件 wordpress/wp-content/themes/stardust/comments.php 到 wordpress/wp-content/themes/stardust/guestcomments.php。
使用文本编辑器编辑 wordpress/wp-content/themes/stardust/guestcomments.php。
把 “评论” 全部换成 “留言”。
让留言倒序显示
找到
<?php foreach ($comments as $comment) : ?> |
修改为
<?php foreach (array_reverse($comments) as $comment) : ?> |
编辑后的文件如下
<?php // Do not delete these lines if ('comments.php' == basename($_SERVER['SCRIPT_FILENAME'])) die ('Please do not load this page directly. Thanks!'); if (!empty($post->post_password)) { // if there's a password if ($_COOKIE['wp-postpass_' . COOKIEHASH] != $post->post_password) { // and it doesn't match the cookie ?> <p class="nocomments">This post is password protected. Enter the password to view comments.</p> <?php return; } } /* This variable is for alternating comment background */ $oddcomment = 'class="alt" '; ?> <!-- You can start editing here. --> <?php if ($comments) : ?> <h3 id="comments">共有 <?php comments_number('0 条留言', '1 条留言', '% 条留言' );?></h3> <ul class="commentlist"> <?php foreach (array_reverse($comments) as $comment) : ?> <li <?php echo $oddcomment; ?>id="comment-<?php comment_ID() ?>"> <?php if(function_exists('get_avatar')) { echo get_avatar($comment, '32'); } ?> <cite><?php comment_author_link() ?></cite> 说: <?php if ($comment->comment_approved == '0') : ?> <em>Your comment is awaiting moderation.</em> <?php endif; ?> <br /> <small class="commentmetadata"><a href="#comment-<?php comment_ID() ?>" title=""><?php comment_date('Y年 F j日') ?> <?php comment_time() ?></a> <?php edit_comment_link('编辑',' ',''); ?></small> <div class="commentbody"> <div> <?php comment_text() ?> </div> </div> </li> <?php /* Changes every other comment to a different class */ $oddcomment = ( empty( $oddcomment ) ) ? 'class="alt" ' : ''; ?> <?php endforeach; /* end for each comment */ ?> </ul> <?php else : // this is displayed if there are no comments so far ?> <?php if ('open' == $post->comment_status) : ?> <!-- If comments are open, but there are no comments. --> <?php else : // comments are closed ?> <!-- If comments are closed. --> <?php endif; ?> <?php endif; ?> <?php if ('open' == $post->comment_status) : ?> <h3 id="respond">留言</h3> <?php if ( get_option('comment_registration') && !$user_ID ) : ?> <p>You must be <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?redirect_to=<?php the_permalink(); ?>">logged in</a> to post a comment.</p> <?php else : ?> <form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform"> <?php if ( $user_ID ) : ?> <p>用户:<a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php echo $user_identity; ?></a>。 <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?action=logout" title="注销这个用户">注销 »</a></p> <?php else : ?> <p><input type="text" name="author" id="author" value="<?php echo $comment_author; ?>" size="22" tabindex="1" /> <label for="author"><small>姓名 <?php if ($req) echo "(required)"; ?></small></label></p> <p><input type="text" name="email" id="email" value="<?php echo $comment_author_email; ?>" size="22" tabindex="2" /> <label for="email"><small>邮箱 (不会公开) <?php if ($req) echo "(required)"; ?></small></label></p> <p><input type="text" name="url" id="url" value="<?php echo $comment_author_url; ?>" size="22" tabindex="3" /> <label for="url"><small>网站</small></label></p> <?php endif; ?> <p><textarea name="comment" id="comment" cols="100%" rows="5" tabindex="4"></textarea></p> <p><input name="submit" type="submit" id="submit" tabindex="5" value="提交留言" /> <input type="hidden" name="comment_post_ID" value="<?php echo $id; ?>" /> <?php do_action('comment_form', $post->ID); ?></p> </form> <?php endif; // If registration required and not logged in ?> <?php endif; // if you delete this the sky will fall on your head ?> |
2. 增加留言本页面
登陆 WordPress 后台,撰写一个新的页面,名称为“留言”。页面模板选择“Guestbook”。
3. 根据自己的实际需要修改模板
上面说的是留言本的大概框架的修改。
还有很多细节的地方需要自己根据实际需要进行修改。
修改后的主题下载。
实际效果参见我的博客的“留言”。