WordPress应用自定义文章类型完成恣意模板的办法

本文实例讲述了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
*/
?>

4、注册自定义函数

在刚刚创立的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' );
?>

5、增加函数性能

把上面这段代码增加到 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 );

7、完成该函数的性能
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; ?>

如今,咱们曾经经过循环创立了一个根本的页面模板。这个  函数检索自定义文章类型的元素,并在循环中应用它们。如今自定义文章类型差不多就好了,剩下的就是css款式了。

上述代码可点击此处本站下载

心愿本文所述对大家基于wordpress的网站建立有所协助。

以上就是安达网络工作室关于《WordPress使用自定义文章类型实现任意模板的方法》的一些看法。更多内容请查看本栏目更多内容!

版权声明:本文为 安达网络工作室 转载文章,如有侵权请联系我们及时删除。
相关文章
WordPress 增加Meta Box的办法

置信很多站长都晓得并且曾经在应用WordPress自定义字段,很多插件也应用了这一性能。 自定义字段是一个十分...

修正PHP脚本使WordPress阻拦渣滓评论的办法示例

阻拦英文渣滓评论 因为绝大少数的渣滓评论都是英文的,所以国际不少冤家在应用 Some Chinese Please 插件,...

WordPress主题中增加文章列表页页码导航的PHP代码实例

WordPress 默许给主题开发者的倡议是在文章列表底部提供上下页按钮,所以没有提供间接用在文章列表下的分页...

wordpress自带的缓存性能应用引见

用动态化当然能够处理这些成绩,不过关于流量不大的博客就没必要了。 wordpress自带有缓存体系,要害的函数...

在Ubuntu 14.04上部署 PHP 环境及 WordPress

软件及版本抉择 Ubuntu 14.04 Ubuntu 是目前用户数量首屈一指的发行版,面前有大土豪保护,能够说是轻量级用...

wordpress设置友谊链接只在首页显示的办法

关于有的主题,当从后盾小工具中增加友谊链接后,却发现其是为全站显示的。假如只心愿让它在首页显示,则能...

需求提交

客服服务

亿鸽在线客服系统