add_theme_support() 函數(shù)用于在我們的當(dāng)前使用的主題添加一些特殊的功能,函數(shù)一般寫在主題的functions.php文件中。
語(yǔ)法結(jié)構(gòu)
<?php add_theme_support( $feature ); ?>
參數(shù)
$feature
(string) (必須) 需要添加特殊功能名稱,可以是以下參數(shù):
‘post-thumbnails’ —– 增加縮略圖支持
‘a(chǎn)utomatic-feed-links’ 自動(dòng)輸出RSS
‘post-formats’—– 增加文章格式功能
‘custom-background’—– 增加自定義背景
‘custom-header’—– 增加自定義頂部圖像
‘menus’——自定義導(dǎo)航菜單
post-thumbnails(啟用文章縮略圖功能)
從WordPress2.9版本開(kāi)始,可以給模板添加文章縮略圖功能,操作方法很簡(jiǎn)單,只需要把下面的代碼添加到functions.php里面。也可以使用wp_get_attachment_image_src()函數(shù)來(lái)實(shí)現(xiàn)前端添加縮略圖功能。
//后臺(tái)增加特色圖像功能
add_theme_support('post-thumbnails');
//然后在要顯示縮略圖的地方放置下面的代碼即可。
if(has_post_thumbnail()){
the_post_thumbnail();
}
//僅在post中使用縮略圖功能
add_theme_support( 'post-thumbnails', array( 'post' ) );
//僅在page中使用縮略圖功能
add_theme_support( 'post-thumbnails', array( 'page' ) );
//僅在 post 和 movies 中使用
add_theme_support( 'post-thumbnails', array( 'post', 'movies' ) );
post-formats添加支持文章格式
add_theme_support( 'post-formats', array(
'aside',
'chat',
'gallery',
'image',
'link',
'quote',
'status',
'video',
'audio'
)
);
WordPress支持以下十個(gè)文章格式
Standard:只是一個(gè)普通的文章沒(méi)有什么特別的東西
Aside:類似于一個(gè)facebook的更新
Chat:全文聊天
Image:只是一個(gè)簡(jiǎn)單的圖像,沒(méi)有什么巨大的
Link:鏈接到外部網(wǎng)站
Quote:引用
Status:一個(gè)簡(jiǎn)短的狀態(tài)更新,類似于微博
Video:一個(gè)視頻
Audio:音頻文件
使用文章模板的方法
如果你只是想改變文章循環(huán)的不同的展現(xiàn)方式,只需要添加下面的代碼到你的single.php中:
<?php get_template_part( 'content', get_post_format() ); ?>
現(xiàn)在,創(chuàng)建并且上傳你的自定義格式循環(huán)文件到你正在使用的主題下,文件的命名應(yīng)該為content-{post-format}.php,例如:content-video.php 和 content-audio.php
最后不要忘記添加一個(gè)content.php文件,因?yàn)檫@將作為剛才的自定義格式循環(huán)文件的默認(rèn)文件,如果自定義文件不存在,則使用自定義single.php文件。
如果你是一個(gè)新手,不想去折騰那些煩人的循環(huán),那么只需要?jiǎng)?chuàng)建一個(gè)自定義音頻文章格式,我們將給他命名為:single-video.php。接下來(lái),上傳single-video.php到你的主題的根目錄下面,并且添加如下代碼片段到functions.php中
add_action('template_include', 'load_single_template');
function load_single_template($template) {
$new_template = '';
// 文章模板
if(is_single()){
global $post;
// 音頻模板
if (has_post_format('video')){
// use template file single-video.php for video format
$new_template = locate_template(array('single-video.php'));
}
}
return ('' != $new_template) ? $new_template : $template;
}
現(xiàn)在,你就可以使用 single-video.php 這個(gè)文件作為你的文章形式了,在發(fā)布文章的時(shí)候選擇它就OK了??偟膩?lái)說(shuō),最后這方法比較簡(jiǎn)單,也比較容易懂。