Home >Backend Development >PHP Tutorial >Function in WordPress D8 theme to capture the first image of an article and display it

Function in WordPress D8 theme to capture the first image of an article and display it

WBOY
WBOYOriginal
2016-07-25 09:13:041178browse

取自 WordPress D8 主题;

路径 themed8modules ;

  1. if ( ! function_exists( 'deel_thumbnail' ) ) :
  2. function deel_thumbnail() {
  3. global $post;
  4. if ( has_post_thumbnail() ) {
  5. $domsxe = simplexml_load_string(get_the_post_thumbnail());
  6. $thumbnailsrc = $domsxe->attributes()->src;
  7. echo ''.trim(strip_tags( $post->post_title )).'';
  8. } else {
  9. $content = $post->post_content;
  10. preg_match_all('//sim', $content, $strResult, PREG_PATTERN_ORDER);
  11. $n = count($strResult[1]);
  12. if($n > 0){
  13. echo ''.trim(strip_tags( $post->post_title )).'';
  14. }else {
  15. echo ''.trim(strip_tags( $post->post_title )).'';
  16. }
  17. }
  18. }
  19. endif;
复制代码



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn