
使用wordpress建设网站需要调用各种不同的函数,这样也方便我们建站。这里说下wordpress的截取功能跟,主要是在首页显示内容的时候要很好的控制文本的长度避免内容过长会破坏首页的美观并且在末尾显示“阅读更多”的标签提示还带省略号,这个截取功能使用是最多的而且也是最实用的。
这里有三种方法可以使用:
方法一:想要在截取控制内容长度的地方插入一下代码:
[code]<?php echo substr(get_the_excerpt(), 0,30); ?>[/code]
“30”这个数字就是长度,可以自行修改。
方法二:在function.php中加入以下代码:
[code]function new_excerpt_length($length) {
return 100;
}
add_filter(‘excerpt_length’, ‘new_excerpt_length’);
function new_excerpt_more( $more ) {
return ‘… ‘;
}
add_filter(‘excerpt_more’, ‘new_excerpt_more’);[/code]
以后在想要调用的地方插入:
[code]<?php the_excerpt(); ?>[/code]
方法三:自定义截取法,这个方法可以跟其他截取的内容独立开来相互不影响,在function.php文件中插入一下代码:
[code]function dynamic_excerpt($length) {
global $post;
$text = $post->post_excerpt;
if ( ” == $text ) {
$text = get_the_content(”);
$text = apply_filters(‘the_content’, $text);
$text = str_replace(‘]]>’, ‘]]>’, $text);
}
$text = strip_shortcodes($text);
$text = strip_tags($text);
$text = mb_substr($text,0,$length).’ …’;
echo $text;
}[/code]
调用方法: [code]<?php dynamic_excerpt(30); ?>[/code]