国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > CMS > Wordpress > 正文

wordpress自動調用文章縮略圖的方法總結

2024-09-07 00:51:05
字體:
來源:轉載
供稿:網友

一、自動顯示文章第一張圖片

在當前使用的主題模板的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.     } 

在當前主題模板的index.php文件的內容代碼前或后添加以下代碼:

<?php echo catch_that_image() ?>

二、文章列表頁自動調用文章縮略圖

在外觀–編輯里頭找到functions.php,加入以下這個函數:

  1. function emtx_auto_thumbnail($pID,$thumb='thumbnail') {  
  2. $blogimg = FALSE;  
  3.  if (has_post_thumbnail()) {// 判斷該文章是否已經設置了“特色圖像”,如果有則直接顯示該特色圖像的縮略圖  
  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")) {//如果文章沒有設置特色圖像,則查找文章內是否有上傳圖片 
  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.  }  

然后在相應的模板文件里面調用縮略圖的地方做個修改,把原來調用the_post_thumbnail的地方按照實際需求改為諸如下面這樣的代碼即可:

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

三、后臺所有文章列表顯示縮略圖

打開你主題的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.     // 文章頁調用 
  39.     add_filter( 'manage_posts_columns''fb_AddThumbColumn' ); 
  40.     add_action( 'manage_posts_custom_column''fb_AddThumbValue', 10, 2 ); 
  41.      
  42.     // 頁面調用 
  43.     add_filter( 'manage_pages_columns''fb_AddThumbColumn' ); 
  44.     add_action( 'manage_pages_custom_column''fb_AddThumbValue', 10, 2 ); 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 弋阳县| 邯郸市| 教育| 五寨县| 舞阳县| 定襄县| 玉林市| 即墨市| 大悟县| 新邵县| 梁平县| 莱芜市| 海安县| 商城县| 彭州市| 那坡县| 上高县| 上虞市| 临潭县| 鹤壁市| 中阳县| 勐海县| 胶南市| 万山特区| 资兴市| 绥芬河市| 自治县| 和政县| 彰武县| 云浮市| 温泉县| 通化县| 茌平县| 澎湖县| 武川县| 怀化市| 泾川县| 民县| 衡南县| 屯门区| 东乌珠穆沁旗|