wordpress的自定義文章類型是一項(xiàng)很強(qiáng)大的功能,可以自己創(chuàng)建新的文章類型以便在主題中進(jìn)行調(diào)用,這樣也是與其他主題拉開不同的主要方法。
- 代碼來源:詳情
函數(shù)
創(chuàng)建一個(gè)新的 Post Type 需要使用 register_post_type 函數(shù)注,在你主題的 functions.php 文件下調(diào)用該函數(shù):
register_post_type( $post_type, $args );
//$post_type 參數(shù)就是你自定義 Post Type 的名稱。
function my_custom_post_product() {
$args = array();
register_post_type( 'product', $args );
}
add_action( 'init', 'my_custom_post_product' );
參數(shù)很多,為了寫教程方便,只列出比較常用的參數(shù),大體結(jié)構(gòu)如下:
function my_custom_post_site() {
$labels = array(
'name' => _x( '網(wǎng)址導(dǎo)航', 'post type 名稱' ),
'singular_name' => _x( '網(wǎng)址', 'post type 單個(gè) item 時(shí)的名稱,因?yàn)橛⑽挠袕?fù)數(shù)' ),
'add_new' => _x( '新建網(wǎng)址', '添加新內(nèi)容的鏈接名稱' ),
'add_new_item' => __( '新建網(wǎng)址' ),
'edit_item' => __( '編輯網(wǎng)址' ),
'new_item' => __( '新網(wǎng)址' ),
'all_items' => __( '所有網(wǎng)址' ),
'view_item' => __( '查看網(wǎng)址' ),
'search_items' => __( '搜索網(wǎng)址' ),
'not_found' => __( '沒有找到有關(guān)網(wǎng)址' ),
'not_found_in_trash' => __( '回收站里面沒有相關(guān)網(wǎng)址' ),
'parent_item_colon' => '',
'menu_name' => '網(wǎng)址'
);
$args = array(
'labels' => $labels,
'description' => '網(wǎng)址信息',
'public' => true,
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
'has_archive' => true
);
register_post_type( 'site', $args );
}
add_action( 'init', 'my_custom_post_site' );
將上面代碼加到主題?functions.php
?的最下面,進(jìn)入后臺(tái)你會(huì)發(fā)現(xiàn)多出了?site
?選項(xiàng),這樣表示注冊(cè)成功:

這時(shí)候我們可以新建?site
發(fā)表一篇電影類型的文章了。但是這樣與文章類型基本相同,我們需要更多的自定義來完善我們的?site
類型。
添加分類功能需要使用函數(shù)?register_taxonomy,使用方法也很簡(jiǎn)單,跟注冊(cè) Post Type 函數(shù)類似,只不過多了一個(gè)參數(shù)用來指定對(duì)應(yīng)的 Post Type :
register_taxonomy( $taxonomy, $object_type, $args );
就本例而言,可以配置如下常用參數(shù):
function my_taxonomies_site() {
$labels = array(
'name' => _x( '網(wǎng)址分類', 'taxonomy 名稱' ),
'singular_name' => _x( '網(wǎng)址分類', 'taxonomy 單數(shù)名稱' ),
'search_items' => __( '搜索網(wǎng)址分類' ),
'all_items' => __( '所有網(wǎng)址分類' ),
'parent_item' => __( '該網(wǎng)址分類的上級(jí)分類' ),
'parent_item_colon' => __( '該網(wǎng)址分類的上級(jí)分類:' ),
'edit_item' => __( '編輯網(wǎng)址分類' ),
'update_item' => __( '更新網(wǎng)址分類' ),
'add_new_item' => __( '添加新的網(wǎng)址分類' ),
'new_item_name' => __( '新網(wǎng)址分類' ),
'menu_name' => __( '網(wǎng)址分類' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
);
register_taxonomy( 'sitecat', 'site', $args );
}
add_action( 'init', 'my_taxonomies_site', 0 );
添加到主題之后,我們看到出現(xiàn)了熟悉的文章分類功能,只不過上面的文案全部變成我們自定義的內(nèi)容了:

為 Post Type 添加自定義 Meta Box
我們想要添加的電影類型不能僅僅只有正文內(nèi)容,我們還需要額外添加一些 導(dǎo)演 之類的有關(guān)內(nèi)容。那么就需要添加自定義 Meta Box,Meta Box 可以在文章發(fā)表頁面中添加自定義的表單,編寫文章的時(shí)候可以填寫額外的信息然后在前端調(diào)用出來。
自定義 Meta Box 需要用到?add_meta_box?函數(shù):
add_meta_box( $id, $title, $callback, $post_type, $context,$priority, $callback_args );
我們注冊(cè)一個(gè) Meta Box :
add_action( 'add_meta_boxes', 'site_director' );
function site_director() {
add_meta_box(
'site_director',
'網(wǎng)址鏈接',
'site_director_meta_box',
'site',
'side',
'low'
);
}
然后在配置參數(shù)里面指定了回調(diào)函數(shù)?site_director_meta_box
,我們需要在這個(gè)函數(shù)里面創(chuàng)建表單:
function site_director_meta_box($post) {
// 創(chuàng)建臨時(shí)隱藏表單,為了安全
wp_nonce_field( 'site_director_meta_box', 'site_director_meta_box_nonce' );
// 獲取之前存儲(chǔ)的值
$value = get_post_meta( $post->ID, '_site_director', true );
?>
<label for="site_director"></label>
<input type="text" id="site_director" style="width:100%" name="site_director" value="<?php echo esc_attr( $value ); ?>" placeholder="輸入網(wǎng)址鏈接" >
<?php
}
add_action( 'save_post', 'site_director_save_meta_box' );
function site_director_save_meta_box($post_id){
// 安全檢查
// 檢查是否發(fā)送了一次性隱藏表單內(nèi)容(判斷是否為第三者模擬提交)
if ( ! isset( $_POST['site_director_meta_box_nonce'] ) ) {
return;
}
// 判斷隱藏表單的值與之前是否相同
if ( ! wp_verify_nonce( $_POST['site_director_meta_box_nonce'], 'site_director_meta_box' ) ) {
return;
}
// 判斷該用戶是否有權(quán)限
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
// 判斷 Meta Box 是否為空
if ( ! isset( $_POST['site_director'] ) ) {
return;
}
$site_director = sanitize_text_field( $_POST['site_director'] );
update_post_meta( $post_id, '_site_director', $site_director );
}
添加自定義字段:
add_action("manage_posts_custom_column", "site_custom_columns");
add_filter("manage_edit-site_columns", "site_edit_columns");
function site_custom_columns($column){
global $post;
switch ($column) {
case "site_director":
echo get_post_meta( $post->ID, '_site_director', true );
break;
}
}
function site_edit_columns($columns){
$columns['site_director'] = '網(wǎng)址';
return $columns;
}
顯示 Meta Box 內(nèi)容
echo '網(wǎng)址:'.get_post_meta( get_the_ID(), '_site_director', true );
調(diào)用 WP_Query 高度自定義調(diào)用 Post Type 的內(nèi)容
$args = array( 'post_type' => 'site', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '
<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
補(bǔ)充
除了文章本身的欄目以外需要進(jìn)行設(shè)計(jì)作品的展示,所以想添加一個(gè)新的文章類型調(diào)用獨(dú)立的展示模板,所以就用的了wp的自定義文章類型的功能。
- 代碼來源:詳情
網(wǎng)上教程很多,這里不做贅述,在實(shí)現(xiàn)功能后,前臺(tái)需要調(diào)用文章進(jìn)行列表展示,網(wǎng)上很多類似的教程,但是有些不是很完善,這里總結(jié)代碼如下:
<?php
$args = array(
'post_type' => 'portfolio', //自定義文章類型名稱
'showposts' => 10, //輸出的文章數(shù)量,這個(gè)可以是缺省值,不用設(shè)置
//如果你不需要分類,想調(diào)用所有分類法的文章比如首頁,可以去掉下面根據(jù)id調(diào)用這段代碼
'tax_query' => array(
array(
'taxonomy' => 'portfolio_list',//自定義分類法名稱
'terms' => 64 //id 為 64 的分類。也可是多個(gè)分類 array(12,64)
),
)
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();?>
//這里輸出分類文章
<h3><?php the_title(); ?></h3>
<p><?php the_content(); ?></p>
<?php endwhile; wp_reset_query(); //重置 query 查詢
} ?>
搜索
允許自定義文章類型(custom post type)出現(xiàn)在搜索結(jié)果中
// MAKE CUSTOM POST TYPES SEARCHABLE
function searchAll( $query ) {
if ( $query->is_search ) { $query->set( 'post_type', array( 'site','plugin', 'theme','person' )); }
return $query;
}
add_filter( 'the_search_query', 'searchAll' );