麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > CMS > Wordpress > 正文

wordpress自動(dòng)調(diào)用文章縮略圖的方法總結(jié)

2024-09-07 00:51:05
字體:
供稿:網(wǎng)友

一、自動(dòng)顯示文章第一張圖片

在當(dāng)前使用的主題模板的functions.php文件<?php和?>之前添加以下代碼:

  1. function catch_that_image() { 
  2.       global $post$posts
  3.       $first_img = ''
  4.       ob_start(); 
  5.       ob_end_clean(); 
  6.       $output = preg_match_all('/<img.+src=['"]([^'"]+)['"].*>/i', $post->post_content, $matches); 
  7.       $first_img = $matches [1] [0]; 
  8.       if(emptyempty($first_img)){ //Defines a default image 
  9.         $first_img = "/images/default.jpg"
  10.       } 
  11.       return $first_img
  12.     } 

在當(dāng)前主題模板的index.php文件的內(nèi)容代碼前或后添加以下代碼:

<?php echo catch_that_image() ?>

二、文章列表頁自動(dòng)調(diào)用文章縮略圖

在外觀–編輯里頭找到functions.php,加入以下這個(gè)函數(shù):

  1. function emtx_auto_thumbnail($pID,$thumb='thumbnail') {  
  2. $blogimg = FALSE;  
  3.  if (has_post_thumbnail()) {// 判斷該文章是否已經(jīng)設(shè)置了“特色圖像”,如果有則直接顯示該特色圖像的縮略圖  
  4.       $blogimg = wp_get_attachment_image_src(get_post_thumbnail_id($pID),$thumb); 
  5.       $blogimg = $blogimg[0]; 
  6.  } elseif ($postimages = get_children("post_parent=$pID&post_type=attachment&post_mime_type=image&numberposts=0")) {//如果文章沒有設(shè)置特色圖像,則查找文章內(nèi)是否有上傳圖片 
  7.     foreach($postimages as $postimage) { 
  8.        $blogimg = wp_get_attachment_image_src($postimage->ID, $thumb); 
  9.        $blogimg = $blogimg[0]; 
  10.       } 
  11.      } elseif (preg_match('/<img [^>]*src=["|']([^"|']+)/i', get_the_content(), $match) != FALSE) { 
  12.       $blogimg = $match[1]; 
  13.      }  
  14.   if($blogimg) {  
  15. $blogimg = '<a href="'. get_permalink().'"><img src="'.$blogimg.'" alt="'.get_the_title().'"  class="alignleft wp-post-image"  /></a>'
  16.       return $blogimg
  17.    
  18.  }  

然后在相應(yīng)的模板文件里面調(diào)用縮略圖的地方做個(gè)修改,把原來調(diào)用the_post_thumbnail的地方按照實(shí)際需求改為諸如下面這樣的代碼即可:

  1. <?php if(emtx_auto_thumbnail($post->ID) ) {  
  2.   echo emtx_auto_thumbnail($post->ID); 
  3. } ?> 

三、后臺(tái)所有文章列表顯示縮略圖

打開你主題的functions.php文件添加如下代碼:

  1. if ( !function_exists('fb_AddThumbColumn') && function_exists('add_theme_support') ) { 
  2.      
  3.     // 在文章列表頁與頁面列表頁添加縮略圖列表 
  4.     add_theme_support('post-thumbnails'array'post''page' ) ); 
  5.      
  6.     function fb_AddThumbColumn($cols) { 
  7.          
  8.         $cols['thumbnail'] = __('Thumbnail'); 
  9.          
  10.         return $cols
  11.     } 
  12.      
  13.     function fb_AddThumbValue($column_name$post_id) { 
  14.              
  15.             $width = (int) 35; 
  16.             $height = (int) 35; 
  17.              
  18.             if ( 'thumbnail' == $column_name ) { 
  19.                 // thumbnail of WP 2.9 
  20.                 $thumbnail_id = get_post_meta( $post_id'_thumbnail_id', true ); 
  21.                 // image from gallery 
  22.                 $attachments = get_children( array('post_parent' => $post_id'post_type' => 'attachment''post_mime_type' => 'image') ); www.111Cn.net 
  23.                 if ($thumbnail_id
  24.                     $thumb = wp_get_attachment_image( $thumbnail_idarray($width$height), true ); 
  25.                 elseif ($attachments) { 
  26.                     foreach ( $attachments as $attachment_id => $attachment ) { 
  27.                         $thumb = wp_get_attachment_image( $attachment_idarray($width$height), true ); 
  28.                     } 
  29.                 } 
  30.                     if ( isset($thumb) && $thumb ) { 
  31.                         echo $thumb
  32.                     } else { 
  33.                         echo __('None'); 
  34.                     } 
  35.             } 
  36.     } 
  37.      
  38.     // 文章頁調(diào)用 
  39.     add_filter( 'manage_posts_columns''fb_AddThumbColumn' ); 
  40.     add_action( 'manage_posts_custom_column''fb_AddThumbValue', 10, 2 ); 
  41.      
  42.     // 頁面調(diào)用 
  43.     add_filter( 'manage_pages_columns''fb_AddThumbColumn' ); 
  44.     add_action( 'manage_pages_custom_column''fb_AddThumbValue', 10, 2 ); 

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 亚洲一区在线免费视频 | 成熟女人特级毛片www免费 | 久久亚洲精品11p | 日本中文一级片 | 天天躁狠狠躁夜躁2020挡不住 | 久色免费 | 视频一区 中文字幕 | 特黄一区二区三区 | av在线免费观看网站 | 久草在线新时代视觉 | 色婷婷a v| 91九色视频观看 | 欧美hdfree性xxxx | 亚洲免费观看视频 | 久久婷婷一区二区三区 | 九色中文 | 黄色片网站在线免费观看 | av在线浏览 | 欧美人人干 | 黄色毛片视频在线观看 | a视频网站 | 久久艹逼 | wankzhd| 97超级碰碰人国产在线观看 | 多男操一女视频 | 成人福利免费在线观看 | 成年性羞羞视频免费观看无限 | 一本色道久久99精品综合蜜臀 | 久久精品久久久久 | 中文黄色一级片 | 亚洲激情91| 久久蜜桃香蕉精品一区二区三区 | 麻豆视频在线免费观看 | 国产九色在线播放九色 | 日韩激情在线视频 | av日韩一区二区 | 特级黄一级播放 | 国产精品一区在线看 | 91网址在线观看 | 亚洲精品欧美二区三区中文字幕 | 视频一区二区三区在线 |