admin管理员组文章数量:1430688
Nothing I do works. I am using a manual excerpt and when I paste this code into functions.php the words read more appear but they are not a link. Why is this?
add_filter( 'wp_trim_excerpt', 'tu_excerpt_metabox_more' );
function tu_excerpt_metabox_more( $excerpt ) {
$output = $excerpt;
if ( has_excerpt() ) {
$output = sprintf( '%1$s <a href="%2$s">Read more</a>',
$excerpt,
get_permalink()
);
}
return $output;
}
Nothing I do works. I am using a manual excerpt and when I paste this code into functions.php the words read more appear but they are not a link. Why is this?
add_filter( 'wp_trim_excerpt', 'tu_excerpt_metabox_more' );
function tu_excerpt_metabox_more( $excerpt ) {
$output = $excerpt;
if ( has_excerpt() ) {
$output = sprintf( '%1$s <a href="%2$s">Read more</a>',
$excerpt,
get_permalink()
);
}
return $output;
}
Share
Improve this question
asked May 10, 2019 at 3:50
Samantha SheaSamantha Shea
1
4
|
1 Answer
Reset to default 0There are several options you have. You can write your own excerpt, or just use the tools which WordPress offers to you.
There are three options you have:
- Use WordPress core function the_excerpt();
- Use WordPress core function get_the_excerpt();
- Use WordPress filter wp_trim_excerpt();
The easiest and propably most fitting for you is the first one.
<?php
// WordPress template function
the_excerpt();
?>
Excerpt from official WordPress docs:
Displays the excerpt of the current post after applying several filters to it including auto-p formatting which turns double line-breaks into HTML paragraphs. It uses get_the_excerpt() to first generate a trimmed-down version of the full post content should there not be an explicit excerpt for the post.
Now, you want to add a "read more custom link".
Basically, the first step is to add a WordPress filter to the excerpt. The filter we are using is called excerpt_more.
<?php
// Mechanism to add a filter to something
function functionname() {
// Custom code to filter/modify
}
add_filter('filtername', 'functionname');
?>
The next step is to call the excerpt function where you want it.
<?php
// Put these 3 lines of code in your functions.php
function wpdocs_excerpt_more( $more ) {
return '<a href="'.get_the_permalink().'">Permalink</a>';
}
add_filter( 'excerpt_more', 'wpdocs_excerpt_more' );
// This function call needs to be placed within "the loop" of your home.php
the_excerpt();
?>
The function "the_excerpt" needs to be called within the loop. So, make sure you are adding it in the correct place.
本文标签: phpCannot figure out how to add a read more link to a manual excerpt Please help
版权声明:本文标题:php - Cannot figure out how to add a read more link to a manual excerpt. Please help 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745507671a2661320.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
get_permalink()
accepts the post ID as the first parameter, if it is not passed, the function will use the global$post
. Check if global$post
is set. – nmr Commented May 10, 2019 at 6:05get_the_permalink()
is only an alias forget_permalink()
– nmr Commented May 10, 2019 at 8:01