0

我已使用以下代码将 wordpress 中的摘录链接从“[...]”更改为“阅读更多”。我想知道是否有一种方法可以为摘录使用多个链接。假设一个类别的帖子的摘录有“阅读更多”,然后另一个类别的帖子的摘录链接有“查看照片”。这可能吗?

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;
}
4

1 回答 1

1

是的,你可以做到。使用excerpt_more过滤器更改“[...]”和 in_category 函数以检查类别以获取更多文本。

试试这个代码。

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' );
于 2013-05-15T07:47:28.400 回答