WordPress 模板技巧
如何新建模板?
复制index.php为Template.php,并在文件开头添加:
1 2 3 4 5 | <?php
/*
Template Name: My-Template
*/
?> |
新建页面时,选择此模板即可。
如何显示日志摘要?
在<?php if (have_posts()) : ?>前添加<?php global $more; $more=0; ?>。
并用<?php the_content("More..."); ?>显示日志内容。
编辑日志时,还要在合适的位置添加<!--more-->标签。
如果要显示纯文本摘要,则用<?php the_excerpt(); ?>显示日志内容,无需其他设置。
如何按分类显示日志摘要?
如果要在不同页面中显示不同分类的日志摘要,可在<?php if (have_posts()) : ?>前添加:
1 2 3 4 5 | <?php
if (is_page('tech-page')) { query_posts("category_name=tech&paged=$paged"); }
elseif (is_page('life-page')) { query_posts("category_name=life&paged=$paged"); }
else { query_posts("paged=$paged"); }
?> |
如何显示日志的分类路径?
如果要显示日志所在的分类路径,可在标题代码前添加:
1 2 3 4 5 | <div class="post-path"> <a href="<?php bloginfo('url'); ?>" title="首页">首页</a> » <?php $category=get_the_category(); echo get_category_parents($category[0]->cat_ID,true,' » '); ?> <?php the_title(); ?> </div> |
如何显示日志分页?
单篇日志分页,在<?php the_content("More..."); ?>后面添加:
1 2 3 4 5 | <?php
wp_link_pages('before=<p><strong>分页:</strong>&after=&next_or_number=text&previouspagelink=上一页&nextpagelink=');
echo ' '; wp_link_pages('before=&after=&next_or_number=number');
echo ' '; wp_link_pages('before=&after=</p>&next_or_number=text&nextpagelink=下一页&previouspagelink=');
?> |
编辑日志时,还要在合适的位置添加<!--nextpage-->标签。
如何显示日志列表分页?
日志列表分页,可以使用Pagebar2、WP-PageNavi或WP Page Numbers插件。
并在<?php endwhile; ?>后面添加一行,以Pagebar2为例:
1 | <?php if(function_exists('wp_pagebar')) wp_pagebar(); ?> |
如何显示同一个分类中的上一篇/下一篇日志?
在<?php while (have_posts()) : the_post(); ?><?php endwhile; ?>之间合适的位置添加:
1 2 | <?php previous_post_link('上一篇:%link','%title',true) ?> <?php next_post_link('下一篇:%link','%title',true) ?> |
如何截取日志标题?
如果日志标题过长,会影响页面样式,可以将其截短。
首先在functions.php里添加字符串截取函数cut_str():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | function cut_str($src_str,$cut_length) { $return_str=''; $i=0; $n=0; $str_length=strlen($src_str); // 字符串长度 while (($n<$cut_length) && ($i<=$str_length)) { $tmp_str=substr($src_str,$i,1); $ascnum=ord($tmp_str); // 第$i位字节的ASCII码 if ($ascnum>=224) // 如果ASCII码大于224 { $return_str=$return_str.substr($src_str,$i,3); // 根据UTF-8编码规范,将3个连续的字节计为单个字符 $i=$i+3; // 实际字节数为3 $n=$n+2; // 字符串长度计为2 } elseif ($ascnum>=192) // 如果ASCII码大于192 { $return_str=$return_str.substr($src_str,$i,2); // 根据UTF-8编码规范,将2个连续的字节计为单个字符 $i=$i+2; // 实际字节数为2 $n=$n+2; // 字符串长度计为2 } elseif ($ascnum>=65 && $ascnum<=90) // 如果ASCII码在65-90之间(即大写字母) { $return_str=$return_str.substr($src_str,$i,1); $i=$i+1; // 实际字节数为1 $n=$n+2; // 字符串长度计为2 } else // 其他情况,包括小写字母和半角标点符号 { $return_str=$return_str.substr($src_str,$i,1); $i=$i+1; // 实际字节数为1 $n=$n+1; // 字符串长度计为1 } } if ($i<$str_length) { $return_str = $return_str . '...'; // 超过长度时在末尾加省略号 } if (get_post_status() == 'private') { $return_str = $return_str . '(私人)'; // 私人日志或页面 } return $return_str; } |
如果截取日志标题,用"echo cut_str($post->post_title,50)"代替"the_title()"即可。
如果截取上一篇/下一篇的标题,改为下面的代码即可:
1 2 | <?php previous_post_link('上一篇:%link',cut_str(get_previous_post(true)->post_title,50),true) ?> <?php next_post_link('下一篇:%link',cut_str(get_next_post(true)->post_title,50),true) ?> |
如何显示引用通告地址?
在single.php和page.php中,<?php endwhile; ?>之前合适的位置添加:
1 2 | Trackback: <a href="<?php trackback_url(); ?>" title="<?php trackback_url(); ?>"><?php trackback_url(); ?></a> <!-- <?php trackback_rdf(); ?> --> |
如何显示评论?
在<?php endwhile; ?>前添加<?php comments_template(); ?>即可。
如何倒序显示评论?
修改wp-includes/comment-template.php,找到函数comments_template中的所有"ORDER BY comment_date",将其改为"ORDER BY comment_date DESC"即可。
如何从评论中把TrackBack和PingBack分离出来?
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <div id="comments"> <h1 class="comments-title"> <?php if (post_comment_only_count() != 0) { echo '评论 (' . post_comment_only_count() . ')'; } ?> </h1> <?php foreach ($comments as $comment) : ?> <?php $comment_type = get_comment_type(); ?> <?php if ($comment_type == 'comment') : ?> <div class="comment" id="comment-<?php comment_ID() ?>"> ...... </div> <?php endif; ?> <?php endforeach; ?> <h1 class="comments-title"> <?php if (post_trackback_pingback_count() != 0) { echo '引用 (' . post_trackback_pingback_count() . ')'; } ?> </h1> <?php foreach ($comments as $comment) : ?> <?php $comment_type = get_comment_type(); ?> <?php if ($comment_type == 'trackback' || $comment_type == 'pingback') : ?> <div class="comment" id="comment-<?php comment_ID() ?>"> ...... </div> <?php endif; ?> <?php endforeach; ?> </div> |
还要在functions.php里添加评论数和引用数统计函数,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 | <?php function post_comment_only_count() { global $wpdb, $id; $request = "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = '$id' AND comment_approved = '1' AND comment_type=''"; return $wpdb->get_var($request); } function post_trackback_pingback_count() { global $wpdb, $id; $request = "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = '$id' AND comment_approved = '1' AND (comment_type='trackback' OR comment_type='pingback')"; return $wpdb->get_var($request); } ?> |
如何高亮显示分类导航菜单?
代码如下,参考Dynamic Menu Highlighting:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <div id="nav"> <ul> <li <?php if (is_home()) echo 'class="current_page_item"'; ?>> <a href="<?php bloginfo('url'); ?>/" title="首页">首页</a> </li> <?php $pages = get_pages('sort_column=menu_order'); foreach ($pages as $page) { if ($page->post_parent == '0') { if (is_page() && $page->ID == $post->ID || (is_single() || is_page() || is_category()) && ( $page->post_name == 'tech' && (rsc_is_page_or_subpage('22') || in_category_or_subcategory_of('6')) || $page->post_name == 'life' && (rsc_is_page_or_subpage('32') || in_category_or_subcategory_of('8')) )) { echo '<li class="current_page_item">'; } else { echo '<li>'; } echo '<a href="' . get_bloginfo('url') . '/' . get_page_uri($page->ID) . '" title="' . $page->post_title . '">' . $page->post_title . '</a>'; echo "</li>\n"; } } ?> </ul> </div> |
还要添加插件Subpage Functions,并在functions.php里添加判断是否属于某个分类的函数。
此函数来自http://comox.textdrive.com/pipermail/wp-hackers/2006-June/006693.html。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php function in_category_or_subcategory_of($cat_id=0) { $cats = get_the_category(); if ( !count($cats) ) // error: no cats defined! return false; foreach ( (array) $cats as $cat ) { if ( $cat->cat_ID == $cat_id ) // is specifically in $cat_id return true; if ( in_category_dig_parents($cat->category_parent, $cat_id) ) // is in a child of $cat_id return true; } return false; } function in_category_dig_parents($cat_id, $look_for) { if ( !$cat_id ) // we got to the top category, and there was no match return false; if ( $cat_id == $look_for ) // found $cat_id return true; $cat = get_category($cat_id); return in_category_dig_parents($cat->category_parent, $look_for); // go up a level, and keep trying } ?> |
如何用WP_Query创建模板?
示例代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php /* Template Name: Tech */ <?php global $more; $more = 0; ?> <?php $temp = $wp_query; $wp_query= null; $wp_query = new WP_Query(); ?> <?php $wp_query->query("category_name=tech&paged=$paged"); ?> <?php if ($wp_query->have_posts()) : ?> <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?> ...... <?php endwhile; ?> <?php else : ?> <p>对不起,您请求的页面不存在。</p> <?php endif; ?> <?php $wp_query = null; $wp_query = $temp; ?> |












引用 (1)
[...] 编辑日志时,在合适的位置添加<!--more-->标签。 如果在自建模板中显示,则模板文件中必须有相应的语句支持,参考WordPress模板技巧。 [...]
评论 (1)
看到贵站是一种缘分.回复一下是一种礼貌。站长好啊。以后多多交流。
有空也来我的小站留脚印。
[回复]