WordPress D8 主题当中截取文章首图并显示的函数
2014-04-28 13:33 kksir 阅读(430) 评论(0) 收藏 举报取自 WordPress D8 主题;
路径 theme\d8\modules ;
if ( ! function_exists( 'deel_thumbnail' ) ) : function deel_thumbnail() { global $post; if ( has_post_thumbnail() ) { $domsxe = simplexml_load_string(get_the_post_thumbnail()); $thumbnailsrc = $domsxe->attributes()->src; echo '<img src="'.$thumbnailsrc.'" alt="'.trim(strip_tags( $post->post_title )).'" />'; } else { $content = $post->post_content; preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER); $n = count($strResult[1]); if($n > 0){ echo '<img src="'.$strResult[1][0].'" alt="'.trim(strip_tags( $post->post_title )).'" />'; }else { echo '<img src="'.get_bloginfo('template_url').'/img/thumbnail.png" alt="'.trim(strip_tags( $post->post_title )).'" />'; } } } endif;
浙公网安备 33010602011771号