Use Wordpress' Excerpt with a “more” link?

若如初见. 提交于 2021-02-18 06:43:25

问题


Wordpress' documentation suggests adding the following to functions.php to enable what I want to do:

function new_excerpt_more($post) {
    return '<a href="'. get_permalink($post->ID) . '">' . 'Read the Rest...' . '</a>';
}
add_filter('excerpt_more', 'new_excerpt_more');

As per: http://codex.wordpress.org/Function_Reference/the_excerpt

But when I add this to functions.php, and I attempt to use it, I don't see the more link. Here is how I try to use it:

the_excerpt(__('(more...)'));

I've also tried:

the_excerpt();

Update: I've tried the following, but it either returns an error (if no arguments), or it doesn't display any excerpt or anything (if an argument):

function new_excerpt_more($excerpt) {
    $link = get_permalink();
    $title = the_title('','',false);
    $ahref = '<a href="'.$link.'" title="'.$title.'">more...</a>';
    return str_replace('[...]', $ahref, $excerpt);
}
add_filter('wp_trim_excerpt', 'new_excerpt_more');

回答1:


function new_excerpt_more($output) {
    return $output . '<p><a href="'. get_permalink() . '">' . 'Read the Rest...' . '</a></p>';
}
add_filter('get_the_excerpt', 'new_excerpt_more');

Works with:

<?php the_excerpt(); ?>



回答2:


function new_excerpt_more( ) {
return ' <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">ReadMore</a>';
 }
add_filter( 'excerpt_more', 'new_excerpt_more' );

works with

the_excerpt();



回答3:


This should be what you're looking for:

function new_excerpt_more($excerpt) {
    $link = get_permalink();
    $title = the_title('','',false);
    $ahref = '<a href="'.$link.'" title="'.$title.'">more...</a>';
    return str_replace('[...]', $ahref, $excerpt);
}
add_filter('wp_trim_excerpt', 'new_excerpt_more');



回答4:


A much better solution for people using WordPress 2.9 and higher is to utilize the excerpt_more filter. Using the code below should help you accomplish what you need.

function new_excerpt_more( $more ) {
    return ' <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">Read More</a>';
}
add_filter( 'excerpt_more', 'new_excerpt_more' );

Additional information can be found in the WordPress Codex here: http://codex.wordpress.org/Function_Reference/the_excerpt#Remove_.5B....5D_string_using_Filters




回答5:


I believe that wordpress recommends using 'the_content()' over 'the_excerpt()'

Hopefully this will help, a simple example could be on your page.php putting something like this:

    <?php  global $more;    
       $more = 0;
       the_content("Read the Rest of " . the_title('', '', false)); ?>


来源:https://stackoverflow.com/questions/3152519/use-wordpress-excerpt-with-a-more-link

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!