WordPress的新功能“文章縮略圖“的好用之處在于它具有非常強的靈活性,你可以隨意選擇在哪里顯示和如何顯示它。想要在博客文章中顯示文章縮略圖,你需要用正確的模板標簽來調用:
<?php the_post_thumbnail(); ?>
當你把上面的代碼加入主循環時,the_post_thumbnail()就會輸出文章縮略圖的標記,并帶上縮略圖的完整版或是預覽版。當然文章縮略圖還有很多很棒的地方。使用上面的方法在博客文章中加入縮略圖之后,讓我們來看看該如何在feed中顯示文章縮略圖。
想在feed中加入文章縮略圖,你需要過濾WordPress的feed功能,在feed-excerpt 和 full-feed 加入必要的模板標簽:
// show post thumbnails in feeds function diw_post_thumbnail_feeds($content) { global $post; if(has_post_thumbnail($post->ID)) { $content = '<div>' . get_the_post_thumbnail($post->ID) . '</div>' . $content; } return $content; } add_filter('the_excerpt_rss', 'diw_post_thumbnail_feeds'); add_filter('the_content_feed', 'diw_post_thumbnail_feeds');
在當前主題的I functions.php文件里加入上面的代碼,在你的feed中每篇文章的前面就會顯示縮略圖。如果你想在文章內容的后面顯示縮略圖,你只需要將第四行的代碼改為下方的代碼,即可。
$content = $content . '<div>' . get_the_post_thumbnail($post->ID) . '</div>';
下一篇 新站PR更新規律總結