post

WordPress博客常用代码

1、文章首页或其他页面显示文章摘要,带有格式

如编辑主题中的index.php页面,搜索到 the_content()

<!--此句显示完整的文章内容-->
<?php the_content('Read the rest of this entry »');?>

替换为

<!--此句显示文章摘要-->
<?php the_excerpt();?>

上面的文章摘要会去掉格式,所以还得需要更改一个获取文章摘要的函数,此函数可通过FTP找到wp-includes目录下的formatting.php文件,搜索 wp_trim_excerpt 函数,将函数替换为下面的函数。

function wp_trim_excerpt($text) {
	if ( '' == $text ) {
		$text = get_the_content('');
		$text = strip_shortcodes( $text );
		$text = apply_filters('the_content', $text);
		$text = str_replace(']]>', ']]>', $text);
		$text = strip_tags($text);
		$excerpt_length =350;
		$encoding='utf-8';
		if(mb_strlen($text,$encoding)>$excerpt_length)
		$text=mb_substr($text,0,$excerpt_length,$encoding).'<a href="' . get_permalink($post->ID)  .'">[阅读全文]</a>';
	}
	return $text;
}

完成上面,再看看效果吧。是不是好看多了,首页不会因为文章内容多,而显的好长了。

2、将分类也作为页面模板,以分类为导航

如主题中有category.php文件,将其复制一份,重命名为category-work.php,以分类页作为模板,也可以index.php页作为模板。因人而异了。
打开category-work.php文件,在顶部粘贴下面的代码

  <?php   /* Template Name: 工作记录  */  ?>

这句代码虽然是注释的,但是确是模板名称,只有加了这句,在页面的模板选项中才可以看到。
然后在category-work.php文件中找到

<?php if (have_posts()) : ?> 

在这句的上面粘贴下面的显示分类的代码

<?php 	
   		$limit = get_option('posts_per_page'); 		
   		$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 		
                query_posts('cat=21&showposts=' . $limit=10 . '&paged=' . $paged); 		
                $wp_query--->is_archive = true; $wp_query->is_home = false;
	?>

在第3行代码中,cat就是分类的ID,limit是每页显示的条数。

后台-分类-打开任意一个分类会看到地址栏中这样的链接地址 //www.izhangheng.com/wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID=41&post_type=post 其中,tag_ID的值就是分类ID,如这个地址的分类ID是41.

完成上面后,就可以通过这个分类创建一个页面了。

3、添加站内导航

function get_sitemap(){
	echo '当前位置:<a href="';  bloginfo('url'); echo  '">';
            bloginfo('name');
        echo '</a> »';
	if( is_single() ){
		$categorys = get_the_category();
		$category = $categorys[0];
		echo( get_category_parents($category->term_id,true,' » ') );
		the_title();
	} elseif ( is_page() ){
		the_title();
	} elseif ( is_category() ){
		single_cat_title();
	} elseif ( is_tag() ){
		single_tag_title();
	} elseif ( is_day() ){
		the_time('Y年Fj日');
	} elseif ( is_month() ){
		the_time('Y年F');
	} elseif ( is_year() ){
		the_time('Y年');
	} elseif ( is_search() ){
		echo $s.' 的搜索结果';
	}
}

主题一般都有functions.php函数模板文件,将上面的函数复制放到函数文件的 <?php ?>标记的内部即可。
在需要的HTML处,添加下面的调用代码:

<?php if(function_exists('get_sitemap')) { get_sitemap();} ?>

4、文章归档显示文章数量

首先需要找到主题中调用文章归档的方法处,具体文件应主题不同而不同,下面附上让文章归档显示文章数量及设置归档数量的函数调用方法,具体方法如下:

<?php wp_get_archives('type=monthly&format=html&show_post_count=1&limit=30'); ?>

5、开启服务器 mail()函数来发送WordPress邮件

yum install sendmail
chkconfig sendmail
/root/lnmp restart
/etc/init.d/sendmail start

Oops,到此完成。

Speak Your Mind

*

· 796 次浏览