dousilie9522 2013-05-15 02:29
浏览 56

the_excerpt()的多个“更多”链接; 在Wordpress中

I've changed my excerpt link in wordpress from "[...]" to "Read More" with the code below. I want to know if there is a way to use more than one link for the excerpt. Let's say for posts in one category have "Read More" for their excerpt, and then posts in another category have "See Photos" for their excerpt link. is this possible?

remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'custom_trim_excerpt');

function custom_trim_excerpt($text) { // Fakes an excerpt if needed
global $post;
if ( '' == $text ) {
$text = get_the_content('');
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$text = strip_tags($text);
$excerpt_length = 24;
$words = explode(' ', $text, $excerpt_length + 1);
if (count($words) > $excerpt_length) {
array_pop($words);
array_push($words, '...<br /><a href="'.get_permalink().'" class="tag">Read More</a>');
$text = implode(' ', $words);
}
}
return $text;
}
  • 写回答

1条回答 默认 最新

  • duankang5285 2013-05-15 07:47
    关注

    yes you can do it. use excerpt_more filter to change the "[...]" and in_category function to check the category for read more text.

    try this code.

    function custom_excerpt_more( $more ) {
        $read_more_txt = 'Read More..';
    
        if (in_category('cat_slug'))
            $read_more_txt = 'See Photos..';
        else if (in_category('cat_slug2'))
            $read_more_txt = 'Something else..';
    
        return ' <a title="'. $read_more_txt .'" href="'. get_permalink( get_the_ID() ) .'">'.   $read_more_txt .'</a>'; 
    } 
    add_filter( 'excerpt_more', 'custom_excerpt_more' );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题