自定义Read More
如果你要在网站展示文章摘要,想要让访问者点击标题或者一个连接,鼓励他们继续阅读你的文章,wordpress让种技术变得简单,并且可自定义。
摘要基础
wordpress有2个方法可以显示摘要:
1.使用the_content()函数不变,在发布文章时,使用more标签,按照你希望的那样,截断正在编辑的文章。
2.替换the_content()函数,使用模版标签the_excerpt().
最常见的是第一个方法,但是第二个方法也会被用在模版文件当中,默认情况the_excerpt()会截取55个字符.
Read More 技术
the_content()模版标签函数参数如下:
<?php the_content( $more_link_text, $strip_teaser ); ?>
$more_link_text 设置链接文本,例如“阅读更多”, $strip_teaser 设置是否隐藏“阅读更多”链接,默认参数是FALSE。
使用the_excerpt()函数展示“阅读更多”
有时候想要所有文章显示Read More,这样就不能手动的在文章种使用more标签,此时使用the_excerpt()更合适
the_excerpt() 会输出“[...]”,而不会输出“more...”
为了让the_excerpt()函数显示“阅读更多”,需要写一个自定义函数:
<?php // Replaces the excerpt "more" text by a link function new_excerpt_more($more) { global $post; return '<a class="moretag" href="'. get_permalink($post->ID) . '"> Read the full article...</a>'; } add_filter('excerpt_more', 'new_excerpt_more'); ?>
自定义“more...”文字
仅仅想改变文字:
<?php the_content('Read on...'); ?>
添加标签:
<?php the_content('<span class="moretext">...on the edge of your seat? Click here to solve the mystery.</span>'); ?>
使用get_the_title()
<?php the_content("...continue reading the story called " . get_the_title('', '', false)); ?>
展示:
and I told him that he should get moving or I'd be on him like a limpet. He looked at me with shock on his face and said ...continue reading the story called A Tale That Must Be Told