首页 > 学院 > 开发设计 > 正文

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

2019-11-15 01:34:01
字体:
来源:转载
供稿:网友
WordPRess D8 主题当中截取文章首图并显示的函数 2014-04-28 13:33 by kksir, ... 阅读, ... 评论, 收藏, 编辑

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


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表