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();?> <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; ?>
现在,我们已经通过循环创建了一个基本的页面模板。这个 函数检索自定义文章类型的元素,并在循环中使用它们。现在自定义文章类型差不多就好了,剩下的就是css样式了。
(资源库 www.zyku.net)
上一篇:WordPress实现回复文章评论后发送邮件通知的功能
栏 目:WordPress
下一篇:WordPress后台地址被改导致无法登录后台的简单解决方法
本文标题:WordPress使用自定义文章类型实现任意模板的方法
本文地址:https://www.zyku.net/wp/1199.html
您可能感兴趣的文章
- 07-31帝国cms模板list.var使用程序代码
- 07-31帝国CMS怎么调用自定义列表名称
- 07-31帝国CMS列表模板信息列表使用程序代码实现推荐、 有图
- 04-08使用Jquery.Validate通过正则表达式自定义验证规则汇
- 08-17DedeCMS的checkbox多选字段自定义取值的方法
- 08-10Finecms模板常用调用标签整理
- 08-03阿里云创建自定义镜像的方法以及如何使用自定义镜像
- 12-18蓝色网站后台管理员登录html模板
- 11-20自适应选项卡切换微信支付宝二维码打赏模板
- 11-20jQuery点击弹出支付宝微信二维码扫码打赏模板
- 01-13智慧什郁-智慧什郁应用软件功能介绍
- 03-10帝国CMS灵动标签实现当前栏目高亮支持
- 08-09织梦去除当前位置position导航中最后
- 11-28pre 标签内容自动换行的方法
- 07-15织梦模板调用最新文章标签、推荐内容
- 11-22Navicat整个数据库搜索某个字符串在哪
- 07-18小米12sPro设置省电模式方法
- 03-29BOSS直聘简历上传教程
- 09-14微信查找企业微信入口流程一览
- 02-17华为nova8关闭杂志锁屏设置方法
最近更新
阅读排行
猜你喜欢
- 07-15帝国cms模板内容页用到的if判断语句
- 04-26腾讯地图设置线路偏好步骤教程
- 07-05Linux arpwatch命令
- 03-04wordpress如何去除顶部的工具条(admin
- 01-09蜜蜂漫画-蜜蜂漫画应用软件功能介绍
- 01-11宝航物流货主端-宝航物流货主端应用软
- 01-13函纪请柬-函纪请柬应用软件功能介绍
- 03-13WordPress文章内容中间插入广告的实现
- 01-11一起来翻译-一起来翻译应用软件功能介
- 12-18HTML <ruby> 标签