【发布时间】:2016-11-21 13:12:20
【问题描述】:
我正在尝试将此自定义摘录添加到博客帖子中,以便它从帖子中获取前 35 个字符并将其作为摘录。但是,该代码适用于所有帖子类型,甚至是自定义帖子类型。
如何仅将此自定义摘录仅应用于博客文章?我尝试将下面的代码包装在以下条件中 - 这是默认博客文章页面设置的内容 - 但没有奏效。
if(is_page_template('template-blog-list.php')) {
function custom_excerpts($content = false) {
global $post;
$content = wp_strip_all_tags($post->post_content);
$excerpt_length = 35;
$words = explode(' ', $content, $excerpt_length + 1);
if(count($words) > $excerpt_length) :
array_pop($words);
array_push($words, '...');
$content = implode(' ', $words);
endif;
$content = $content . '<br><br><a class="more-button" href="'. get_permalink($post->ID) . '">Read More</a>';
return $content;
}
add_filter('get_the_excerpt', 'custom_excerpts');
}
【问题讨论】:
标签: wordpress custom-post-type