Skip to content →

WordPress D8 主题当中截取文章首图并显示的函数

取自 WordPress D8 主题;

路径 theme\d8\modules ;

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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;

Published in 编程语言

Comments

发表评论

电子邮件地址不会被公开。 必填项已用*标注