我将“继续阅读”链接功能从“211”主题中移除,并且在大多数情况下,它是有效的(在RSS提要中工作)。
我使用get_posts
, 然而,“继续阅读”链接只是链接到首页,而不是贴子页。
function gavsiu_continue_reading_link() {
return \' <a href="\'. esc_url( get_permalink() ) . \'">\' . __( \'Read more <span class="meta-nav">»</span>\' ) . \'</a>\';
}
function gavsiu_auto_excerpt_more( $more ) {
return \' …\' . gavsiu_continue_reading_link();
}
add_filter( \'excerpt_more\', \'gavsiu_auto_excerpt_more\' );
function gavsiu_custom_excerpt_more( $output ) {
if ( has_excerpt() && ! is_attachment() ) {
$output .= gavsiu_continue_reading_link();
}
return $output;
}
add_filter( \'get_the_excerpt\', \'gavsiu_custom_excerpt_more\' );
短代码:
function gavsiu_recent_posts( $atts ) {
extract( shortcode_atts( array(
\'cat\' => 3,
\'max_posts\' => 1
), $atts ) );
$recent_posts = get_posts( array(
\'category\' => "{$cat}",
\'numberposts\' => "{$max_posts}"
) );
foreach($recent_posts as $recent_post) : setup_postdata($recent_post);
$post = \'<h4 class="recent-post-title c0">\' . $recent_post->post_title . \'</h4>\';
$post .= \'<p class="recent-content c0">\' . get_the_excerpt() . \'</p>\';
return $post;
endforeach;
}
add_shortcode( \'recent_posts\', \'gavsiu_recent_posts\' );
最合适的回答,由SO网友:gavsiu 整理而成
function gavsiu_recent_posts( $atts ) {
extract( shortcode_atts( array(
\'cat\' => 3,
\'max_posts\' => 1
), $atts ) );
$recent_posts = get_posts( array(
\'category\' => "{$cat}",
\'numberposts\' => "{$max_posts}",
\'offset\' => 0
) );
foreach($recent_posts as $post) : setup_postdata($post);
$permalink = \' <a href="\'. esc_url( get_permalink( $post->ID ) ) . \'" title="Permalink to \' . esc_attr( $post->post_title ) . \'" rel="bookmark">\' . __( \'Read more <span class="meta-nav">»</span>\' ) . \'</a>\';
$return = \'<h4 class="recent-post-title">\' . $post->post_title . "</h4>\\n";
if ( $post->post_excerpt == \'\' ) :
$return .= preg_replace( array( \'/ <a href="http:\\/\\/gavsiu.com\\/" title="Permalink to ".*?<\\/a>/\', \'/<p .*?>/\' ), array( $permalink, \'<p>\' ), get_the_excerpt() ) . "\\n";
else :
$return .= \'<p>\' . $post->post_excerpt . $permalink . "</p>\\n";
endif;
return $return;
endforeach;
}
SO网友:Chris_O
自调用setup\\u postdata后,需要在get\\u posts循环后重置post数据。最好同时删除setup\\u postdata。
global $post;
$recent_posts = get_posts( $yourargshere );
foreach($recent_posts as $post) :
$return = \'<h4 class="recent-post-title c0">\' . $post->post_title . \'</h4>\';
$return .= \'<p class="recent-content c0">\' . apply_filters( \'the_excerpt\', $post->post_excerpt ) . \'</p>\';
return $return;
endforeach;
您还需要将add\\u筛选器更改为\\u摘录,而不是获取\\u摘录