內(nèi)容類型 - Custom Post Types
1.自定義內(nèi)容類型
- 文章:posts
- 頁面:pages
自定義文章類型幫助我們將不同類型的文章放在不同的容器中,它可以將我們的文章和其他的區(qū)別開。
register_post_type():注冊(cè)內(nèi)容類型,第一個(gè)參數(shù)是定義了自定義文章類型的名字;第二個(gè)是一個(gè)數(shù)組,定義新的自定義文章類型的屬性。
2. 自定義內(nèi)容類型的標(biāo)簽與更新提示信息
/*
* 自定義內(nèi)容類型-電影
*/
function movietalk_custom_post_type_movie(){
$labels = array(
// 菜單頁面顯示的文字
'name' => '電影',
'singular_name'=> '電影',
'add_new' => '添加電影',
'add_new_item'=> '添加電影資料',
'edit_item' => '編輯電影資料',
'new_item' => '新電影',
'all_items' => '所有電影',
'view_item' => '查看電影',
'search_items' => '搜索電影',
'not_found' => '沒有找到電影資料',
'not_found_in_trash' => '回收站里沒找到電影資料',
'menu_name' => '電影',
);
$args = array(
'public' => true,
'labels' => $labels,
);
register_post_type('movie', $args);
// 'movie':文章類型的名稱
// $args:自定義文章類型配置數(shù)組
}
add_action('init', 'movietalk_custom_post_type_movie');
// 將函數(shù)連接到指定action(動(dòng)作)
3. 自定義內(nèi)容類型的更多參數(shù)
'menu_postion' => 5, // 菜單項(xiàng)所在位置
'supports' => array('title', 'editor', 'thumnail', 'excerpt', 'custom-fields','revisions'),
// 對(duì)文章類型的一些功能支持(標(biāo)題、作者、特色圖像、摘要、自定義字段等)
4. 自定義內(nèi)容類型的地址重寫
'has_archive' => true, // 文章是否有歸檔頁面
'rewrite' => array('slug' => 'film', 'with_front' => false),
// 是否url重寫
插件 》Custom Post Type Permalinks:自定義文章類型固定鏈接設(shè)置插件
5. 使用頁面模板來顯示自定義內(nèi)容列表
自定義內(nèi)容列表模板文件
// page-movie.php
<?php
/*
* Template Name: 電影頁面
*/
get_header(); ?>
<div class="entry-content">
<?php query_posts('post_type=movie'); ?>
<!-- query_posts():放在LOOP之前,限定循環(huán)所需要的條件 -->
<?php while(have_posts()):the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; ?>
</div>
分類法
1. 自定義分類法
分類法就是對(duì)內(nèi)容進(jìn)行分類的方法,WP提供了兩種,一種叫category分類,另一種叫tag標(biāo)簽;分類可以擁有層級(jí)關(guān)系,而標(biāo)簽沒有層級(jí)關(guān)系。
創(chuàng)建更多的分類法,然后分配到指定的內(nèi)容類型上去使用
/*
* 自定義分類法 - 電影類型
*/
function movietalk_custom_taxonomy_genre(){
$labels = array(
'name' => '類型',
'singular_name' => '類型',
'search_items' => '搜索類型',
'popular_itmes' => '熱門類型',
'all_items' => '所有類型',
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => '編輯類型',
'update_item' => '更新類型',
'add_new_item' => '添加新類型',
'separate_items_with_commas' => '使用逗號(hào)分割不同的類型',
'add_or_remove_items' => '添加或移除類型',
'choose_from_most_used' => '從使用最多的類型里選擇',
'menu_name' => '類型',
);
$args = array(
'public' => true,
'labels' => $labels,
);
register_taxonomy('genre', 'movie', $args);
register_taxonomy($taxonomy, $object_type, $args);
//$taxonomy 要注冊(cè)的分類法的名稱
//$object_type 分類法所對(duì)應(yīng)的文章類型
//$args 相關(guān)參數(shù)
}
add_action('init', 'movietalk_custom_taxonomy_genre');
--
字段
1. 自定義字段
WP本身就有自定義字段功能,在發(fā)布內(nèi)容的時(shí)候,我們可以定義一段額外的字段,然后為這些字段輸入響應(yīng)的內(nèi)容。在主題的模板文件里,我們可以用代碼來調(diào)用并顯示這些自定義字段的內(nèi)容。
插件 》Advanced Custom Fields:我們可以創(chuàng)建不同的字段組,然后分配到指定的內(nèi)容類型上去使用。
菜單欄 》字段 》新建 》添加字段
規(guī)則(創(chuàng)建一組規(guī)則以確定自定義字段在哪個(gè)編輯界面顯示) 》文章類型 == movie
2. 自定義字段的顯示
<?php while(have_posts()) : the_post(); ?>
<div class="entry-entent">
<h1><?php the_title(); ?></h1>
<?php if(get_field('runtime')): ?>
<!-- 判斷是否填寫了runtime字段 -->
<strong>片長:</strong><?php the_field('runtime'); ?>
<!-- 顯示runtime字段 -->
<?php endif; ?>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
3. 圖像字段
字段 》添加字段 》海報(bào)-poster-圖像
返回值:圖像ID
預(yù)覽圖大?。篜oster
前端顯示
// functions.php
add_image_size('poster', 128, 180, true);
// 添加一個(gè)叫poster的縮略圖尺寸,按照寬度128像素,高度180像素并允許裁切,生成縮略圖
// single-movie.php
<!-- 獲取海報(bào)的ID -->
<?php $poster_id = get_field('poster'); ?>
<!-- 若附件是圖像,函數(shù)嘗試返回其縮略圖或適當(dāng)大小的圖像 -->
<?php echo wp_get_attachment_image($poster_id, 'poster'); ?>