HotNews主題不同分類(lèi)顯示不同的隨機(jī)縮略圖

字號(hào):


    HotNews主題集成的隨機(jī)縮略圖本來(lái)是為懶人準(zhǔn)備的,可能有童鞋感覺(jué),不同的分類(lèi)內(nèi)容都顯示相同的隨機(jī)縮略圖,有些不搭調(diào),如果博客日志較多,文章中又無(wú)圖片,重新編輯添加圖片工作量又太大,來(lái)個(gè)折中的辦法,改動(dòng)一下隨機(jī)縮略圖調(diào)用函數(shù),讓不同的分類(lèi)顯示不同的隨機(jī)縮略圖。
    首先,在HotNews主題images目錄新建名稱(chēng)為:random1、random2、random3.........文件夾,并在其中放置不同的隨機(jī)縮略圖片,圖片名稱(chēng)必須是連續(xù)的。
    其次,打開(kāi)主題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í)際自行修改。
    修改上面代碼類(lèi)似“ is_category( '472' )”中數(shù)字為相應(yīng)的分類(lèi)ID號(hào)
    random3是圖片文件夾的名稱(chēng)
    如果分類(lèi)較多,可以多復(fù)制幾個(gè):
    elseif ( is_category( '474' ) ) {
    echo '/images/random3/'.$random.'.jpg';
    }
    同樣要修改其中的分類(lèi)ID及圖片文件夾名稱(chēng)。