使用条件更改readmore链接怎么样。
下面是一个示例代码,它根据帖子的类别返回不同的阅读更多文本。您应该阅读official codex page 了解wordpress中可以使用的其他条件标记的更多信息。
Usage - 将此代码放入主题functions.php 文件此代码将替换continue reading 具有View Image 如果post处于image 类别
<?php
function wpse_60184_new_readmore_link( $more ) {
$read_more_link_2 = "View Image";
if ( in_category( \'image\' )) {
return $read_more_link_2;
}
}
add_filter(\'excerpt_more\', \'wpse_60184_new_readmore_link\');
?>
更新#1将此代码放在主题的末尾
functions.php //this will create read morelink when the_excerpt() is used
function wpse_60184_the_excerpt_more($more) {
in_category( \'events\' ) ? $my_read_more_text = \'Read event\' : $my_read_more_text = \'Read more\';
global $post;
return \'<a href="\'. get_permalink($post->ID) . \'">\'.$my_read_more_text.\'</a>\';
}
add_filter(\'excerpt_more\', \'wpse_60184_the_excerpt_more\');
//this will change the read more link when <!-- more --> is used & the_content()
function wpse_60184_the_content_more( $more_link, $more_link_text ) {
in_category( \'events\' ) ? $my_read_more_text = \'Read events\' : $my_read_more_text = \'Read more\';
return str_replace( $more_link_text, $my_read_more_text, $more_link );
}
add_filter( \'the_content_more_link\', \'wpse_60184_the_content_more\', 10, 2 );
第二段代码是用211主题测试的,它在localhost上运行良好。
wpse_60184_the_excerpt_more 在以下情况下创建“阅读更多”链接the_excerpt() 用于主题wpse_60184_the_content_more 将用新链接替换默认的“读取更多”链接。将使用<!-- more --> 标记