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

取自 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;

 

你可能感兴趣的:(wordpress)