本文实例讲述了WordPress应用自定义文章类型完成恣意模板的办法。分享给大家供大家参考,详细如下:
这几天在搭建博客的时分,碰到了一个对我来说很辣手的成绩。WordPress自带的文章类型只能应用他们特定的模版,而我因为想做一个心境工夫轴的板块,所以只能自定义文章的类型,让他来支持我的这个模版。于是网上找材料,并且以插件的方式来体现,失去了如下的处理计划:
次要就是应用了register_post_type 函数。
1、创立插件目录
新建一个文件夹用来寄存插件文件,这里我就命名这个文件夹为myMood
2、创立PHP代码文件
在方才创立的文件夹外面新建一个php文件,命名为myMood,用来书写插件代码
3、增加头部形容
<?php
/*
Plugin Name: Movie Reviews
Plugin URI: http://wp.tutsplus.com/
Description: Declares a plugin that will create a new post type .
Version: 1.0
Author: Summer
Author URI: http://www.xtwind.com/
License: GPLv2
*/
?>在刚刚创立的php文件代码中,在?>后面增加函数:
add_action( 'init', 'create_myMood' );<?php
/*
Plugin Name: Movie Reviews
Plugin URI: http://wp.tutsplus.com/
Description: Declares a plugin that will create a new post type .
Version: 1.0
Author: Summer
Author URI: http://www.xtwind.com/
License: GPLv2
*/
add_action( 'init', 'create_myMood' );
?>把上面这段代码增加到 add_action( 'init', 'create_myMood' ); 的后面
function create_lsxq() {
register_post_type( 'lsxq',
array(
'labels' => array(
'name' => '零散心境',
'singular_name' => 'lsxq',
'add_new' => '写心境',
'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
)
);
}
对 register_post_type 这个函数收回申明,它就为新的文章类型做好了各种治理性能。这个函数包括两个参数:**个是定义了自定义文章类型的名字 ;第二个是一个数组,用来定义新的自定义文章类型的属性。
**个参数很简略,大家本人领悟。这里简略说下位置个参数:
'public' => true 决议该文章类型在治理后盾和前端的可见性
'menu_position' => 5 决议该文章类型菜单的地位
'supports' => array( 'title', 'editor', 'comments', 'thumbnail') 决议自定义文章类型的性能
'taxonomies' => array( '' ) 创立自定义分类,这里没有定义。
'menu_icon' => plugins_url( 'image.png', __FILE__ ) 显示治理菜单的图标,图标文件放在和插件同一目录,为16*16像素
'has_archive' => true 启用自定义文章类型的存档性能
请拜访 register_post_type 理解更多对于该函数的参数细节。
6、创立一个该自定义文章类型的模版
关上刚刚的代码文件,在
add_action( 'init', 'create_lsxq' );语句后面增加上面这一语句:add_filter( 'template_include', 'include_template_function', 1 );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;
}add_filter( 'template_include', 'include_template_function', 1 );
8、创立单页面模版single-lsxq.php
创立一个名字为single-lsqx.php的文件,次要代码段如下:
<?php
$mypost = array( 'post_type' => 'lsxq', );
$loop = new WP_Query( $mypost );
?>
<?php while ( $loop->have_posts() ) : $loop->the_post();?>
<p class="item">
<h3> <span class="fr"><?php the_time('Y-n-j H:i') ?></span> </h3>
<p class="con">
<?php the_content(); ?>
</p>
</p>
<?php endwhile; ?>上述代码可点击此处本站下载。
心愿本文所述对大家基于wordpress的网站建立有所协助。
以上就是安达网络工作室关于《WordPress使用自定义文章类型实现任意模板的方法》的一些看法。更多内容请查看本栏目更多内容!
get_avatar()(获取头像) get_avatar() 函数用来获取置顶邮箱或许用户的头像代码,在评论列表中十分罕用。...
more标签 这种办法应该是最灵敏的一种办法,操作也很简略,只要要你在编辑文章的时分拔出more标签 或许应用...
comments_template comments_template 函数是一个调用评论模板的函数,应用起来很简略,与get_header()等函...
本文实例讲述了WordPress修正评论默许头像的办法。分享给大家供大家参考。详细剖析如下: 咱们晓得wordpres...
上一篇,咱们应用 Meta Box 为文章增加了一个“推荐指数”字段。增加后用了一段工夫,仿佛不那么...
本文实例讲述了wordpress调用以后分类下子分类的办法。分享给大家供大家参考。详细剖析如下: 本人没用过wo...