這幾天在搭建博客的時(shí)候,,碰到了一個(gè)對(duì)我來(lái)說(shuō)很棘手的問(wèn)題。WordPress自帶的文章類(lèi)型只能使用他們特定的模版,,而我由于想做一個(gè)心情時(shí)間軸的板塊,,所以只能自定義文章的類(lèi)型,讓他來(lái)支持我的這個(gè)模版,。于是網(wǎng)上找資料,,并且以插件的形式來(lái)表現(xiàn),得到了如下的解決方案:
主要就是使用了register_post_type 函數(shù),。
1,、創(chuàng)建插件目錄
新建一個(gè)文件夾用來(lái)存放插件文件,這里我就命名這個(gè)文件夾為myMood
2,、創(chuàng)建PHP代碼文件
在剛才創(chuàng)建的文件夾里面新建一個(gè)php文件,命名為myMood,,用來(lái)書(shū)寫(xiě)插件代碼
3,、添加頭部描述
<?php ?>
4、注冊(cè)自定義函數(shù)
在剛剛創(chuàng)建的php文件代碼中,,在?>前面添加函數(shù):
add_action( 'init', 'create_myMood' );
得到如下代碼:
<?php add_action( 'init', 'create_myMood' );?>
5,、添加函數(shù)功能
把下面這段代碼添加到 add_action( 'init', 'create_myMood' ); 的前面
function create_lsxq() { register_post_type( 'lsxq', array( 'labels' => array( 'name' => '零散心情', 'singular_name' => 'lsxq', 'add_new' => '寫(xiě)心情', 'add_new_item' => '添加一條新心情', 'edit' => 'Edit', 'edit_item' => 'Edit lsxq', 'new_item' => 'New lsxq', 'view' => 'View', 'view_item' => 'View lsxq', 'search_items' => 'Search lsxq', 'not_found' => 'No lsxq found', 'not_found_in_trash' => 'No lsxq found in Trash', 'parent' => 'Parent lsxq' ), 'public' => true, 'menu_position' => 15, 'supports' => array( 'title', 'editor', 'comments', 'thumbnail' ), 'taxonomies' => array( '' ), 'menu_icon' => plugins_url( 'images/image.png', __FILE__ ), 'has_archive' => true ) );}
對(duì) register_post_type 這個(gè)函數(shù)發(fā)出聲明,它就為新的文章類(lèi)型做好了各種管理功能,。這個(gè)函數(shù)包括兩個(gè)參數(shù):第一個(gè)是定義了自定義文章類(lèi)型的名字 ,;第二個(gè)是一個(gè)數(shù)組,用來(lái)定義新的自定義文章類(lèi)型的屬性。
第一個(gè)參數(shù)很簡(jiǎn)單,,大家自己領(lǐng)悟,。這里簡(jiǎn)單說(shuō)下地位個(gè)參數(shù):
'public' => true 決定該文章類(lèi)型在管理后臺(tái)和前端的可見(jiàn)性
'menu_position' => 5 決定該文章類(lèi)型菜單的位置
'supports' => array( 'title', 'editor', 'comments', 'thumbnail') 決定自定義文章類(lèi)型的功能
'taxonomies' => array( '' ) 創(chuàng)建自定義分類(lèi),這里沒(méi)有定義,。
'menu_icon' => plugins_url( 'image.png', __FILE__ ) 顯示管理菜單的圖標(biāo),,圖標(biāo)文件放在和插件同一目錄,為16*16像素
'has_archive' => true 啟用自定義文章類(lèi)型的存檔功能
請(qǐng)?jiān)L問(wèn) register_post_type 了解更多關(guān)于該函數(shù)的參數(shù)細(xì)節(jié),。
6,、創(chuàng)建一個(gè)該自定義文章類(lèi)型的模版
打開(kāi)剛剛的代碼文件,在
add_action( 'init', 'create_lsxq' );
語(yǔ)句前面添加下面這一語(yǔ)句:
add_filter( 'template_include', 'include_template_function', 1 );
7,、實(shí)現(xiàn)該函數(shù)的功能
function include_template_function( $template_path ) { if ( get_post_type() == 'lsxq' ) { if ( is_single() ) { if ( $theme_file = locate_template( array ( 'single-lsxq.php' ) ) ) { $template_path = $theme_file; } else { $template_path = plugin_dir_path( __FILE__ ) . '/single-lsxq.php'; } } } return $template_path;}
該代碼段添加在下面語(yǔ)句的后面
add_filter( 'template_include', 'include_template_function', 1 );
8,、創(chuàng)建單頁(yè)面模版single-lsxq.php
創(chuàng)建一個(gè)名字為single-lsqx.php的文件,主要代碼段如下:
<?php $mypost = array( 'post_type' => 'lsxq', ); $loop = new WP_Query( $mypost ); ?> <?php while ( $loop->have_posts() ) : $loop->the_post();?> <div class="item"> <h3> <span class="fr"><?php the_time('Y-n-j H:i') ?></span> </h3> <div class="con"> <?php the_content(); ?> </div> </div><?php endwhile; ?>
現(xiàn)在,,我們已經(jīng)通過(guò)循環(huán)創(chuàng)建了一個(gè)基本的頁(yè)面模板,。這個(gè) 函數(shù)檢索自定義文章類(lèi)型的元素,并在循環(huán)中使用它們?,F(xiàn)在自定義文章類(lèi)型差不多就好了,,剩下的就是css樣式了,。