hotnews主題不同分類顯示不同的隨機(jī)縮略圖

字號:


    hotnews主題集成的隨機(jī)縮略圖本來是為懶人準(zhǔn)備的,可能有童鞋感覺,不同的分類內(nèi)容都顯示相同的隨機(jī)縮略圖,有些不搭調(diào),如果博客日志較多,文章中又無圖片,重新編輯添加圖片工作量又太大,來個折中的辦法,改動一下隨機(jī)縮略圖調(diào)用函數(shù),讓不同的分類顯示不同的隨機(jī)縮略圖。
    首先,在hotnews主題images目錄新建名稱為:random1、random2、random3.........文件夾,并在其中放置不同的隨機(jī)縮略圖片,圖片名稱必須是連續(xù)的。
    其次,打開主題functions.php模版,找到:
    //支持外鏈縮略圖 
    if ( function_exists('add_theme_support') ) 
    add_theme_support('post-thumbnails'); 
    /*catch first image (post-thumbnail fallback) */ 
    function catch_first_image() { 
    global $post, $posts; 
    $first_img = ''; 
    ob_start(); 
    ob_end_clean(); 
    $output = preg_match_all('/<img.+src=[']([^']+)['].*>/i', $post->post_content, $matches); 
    $first_img = $matches [1] [0]; 
    if(empty($first_img)){ //defines a default image 
    $random = mt_rand(1, 20); 
    echo get_bloginfo ( 'stylesheet_directory' ); 
    echo '/images/random/'.$random.'.jpg'; 
    } 
    return $first_img; 
    } 
    替換為:
    //支持外鏈縮略圖 
    if ( function_exists('add_theme_support') ) 
    add_theme_support('post-thumbnails'); 
    /*catch first image (post-thumbnail fallback) */ 
    function catch_first_image() { 
    global $post, $posts; 
    $first_img = ''; 
    ob_start(); 
    ob_end_clean(); 
    $output = preg_match_all('/<img.+src=[']([^']+)['].*>/i', $post->post_content, $matches); 
    $first_img = $matches [1] [0]; 
    if(empty($first_img)){ //defines a default image 
    $random = mt_rand(1, 20); 
    echo get_bloginfo ( 'stylesheet_directory' ); 
    if ( is_category( '472' ) ) { 
    echo '/images/random1/'.$random.'.jpg'; 
    } elseif ( is_category( '473' ) ) { 
    echo '/images/random2/'.$random.'.jpg'; 
    } elseif ( is_category( '474' ) ) { 
    echo '/images/random3/'.$random.'.jpg'; 
    } 
    } 
    return $first_img; 
    } 
    其中:
    數(shù)字20是隨機(jī)縮略圖數(shù)量,根據(jù)實(shí)際自行修改。
    修改上面代碼類似“ is_category( '472' )”中數(shù)字為相應(yīng)的分類id號
    random3是圖片文件夾的名稱
    如果分類較多,可以多復(fù)制幾個:
    elseif ( is_category( '474' ) ) { 
    echo '/images/random3/'.$random.'.jpg'; 
    } 
    同樣要修改其中的分類id及圖片文件夾名稱。