admin管理员组文章数量:1292243
I'm working on a website that uses a static front page. It also shows the single most recent blog post. This was achieved by creating a page and using a custom page template.
Sometimes, the blog post is too long, so I want to use the_excerpt to automatically shorten it without the need for a more tag.
So far so good. But, the_excerpt doesn't actually create a "read more" link. This is a pretty common problem, so I added:
<?php
function new_excerpt_more($more) {
global $post;
return '... <a href="'. get_permalink($post->ID) . '">continue reading</a>.';
}
add_filter('excerpt_more', 'new_excerpt_more');
?>
to my functions.php file.
I've actually used this code without issue on another site, but for whatever reason, it's not working in this case. My initial guess was that it was because it's being called on a static page.
The website is . The theme is Central by QODE, and I'm using a custom child theme.
EDIT
Adding code from the template page, as per request. This isn't the whole page, but rather just the relevant bit for the news post:
<?php $query = "showposts=1&orderby='date'"; query_posts($query);?>
<?php if(have_posts()) : while ( have_posts() ) : the_post(); ?>
<a href="<?php the_permalink();?>"><?php the_post_thumbnail('home'); ?></a>
<div class="overlay">Latest News</div>
<h4><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h4>
<?php the_excerpt(); ?>
<?php endwhile; ?>
<?php endif; ?>
I'm working on a website that uses a static front page. It also shows the single most recent blog post. This was achieved by creating a page and using a custom page template.
Sometimes, the blog post is too long, so I want to use the_excerpt to automatically shorten it without the need for a more tag.
So far so good. But, the_excerpt doesn't actually create a "read more" link. This is a pretty common problem, so I added:
<?php
function new_excerpt_more($more) {
global $post;
return '... <a href="'. get_permalink($post->ID) . '">continue reading</a>.';
}
add_filter('excerpt_more', 'new_excerpt_more');
?>
to my functions.php file.
I've actually used this code without issue on another site, but for whatever reason, it's not working in this case. My initial guess was that it was because it's being called on a static page.
The website is http://stuandjessproductions. The theme is Central by QODE, and I'm using a custom child theme.
EDIT
Adding code from the template page, as per request. This isn't the whole page, but rather just the relevant bit for the news post:
<?php $query = "showposts=1&orderby='date'"; query_posts($query);?>
<?php if(have_posts()) : while ( have_posts() ) : the_post(); ?>
<a href="<?php the_permalink();?>"><?php the_post_thumbnail('home'); ?></a>
<div class="overlay">Latest News</div>
<h4><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h4>
<?php the_excerpt(); ?>
<?php endwhile; ?>
<?php endif; ?>
Share
Improve this question
edited Feb 11, 2014 at 13:51
convoke
asked Feb 11, 2014 at 3:50
convokeconvoke
2111 gold badge2 silver badges6 bronze badges
2
- what is the code of the page template? – Michael Commented Feb 11, 2014 at 9:50
- I've added some of the code from the template page (the relevant bit). If you want to see the whole thing, I can post it... but it's kinda long. – convoke Commented Feb 11, 2014 at 13:55
1 Answer
Reset to default 18On the post edit page, if you fill Excerpt box with any text, the_excerpt()
function doesn't add read more link
or ...
at the end of the short description at frontend. Read more link is only included if Excerpt is set empty. This is not a bug, it's a default behavior.
The solution is to avoid the excerpt_more
filter to return read more link
, and use the the_excerpt
hook to add read more link.
// excerpt_more should be set the empty.
add_filter( 'excerpt_more', '__return_empty_string', 21 );
function wpse_134143_excerpt_more_link( $excerpt ) {
$excerpt .= sprintf(
'... <a href="%s">%s</a>.',
esc_url( get_permalink() ),
__( 'continue reading' )
);
return $excerpt;
}
add_filter( 'the_excerpt', 'wpse_134143_excerpt_more_link', 21 );
Above code could go to your themes functions.php file.
本文标签: excerptHow can I create a quotRead Morequot link using theexcerpt() on a static front page
版权声明:本文标题:excerpt - How can I create a "Read More" link using the_excerpt() on a static front page? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741543280a2384446.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论