请选择 进入手机版 | 继续访问电脑版

wordpress如何自定义文章类型实现任意模板?附文件下载

[复制链接]
树苗收集系 发表于 2019-11-27 00:04:01 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题
本文实例讲述了WordPress使用自定义文章类型实现任意模板的方法。分享给大家供大家参考,具体如下:

这几天在搭建博客的时候,碰到了一个对我来说很棘手的问题。WordPress自带的文章类型只能使用他们特定的模版,而我由于想做一个心情时间轴的板块,所以只能自定义文章的类型,让他来支持我的这个模版。于是网上找资料,并且以插件的形式来表现,得到了如下的解决方案:

主要就是使用了register_post_type 函数。

1、创建插件目录
新建一个文件夹用来存放插件文件,这里我就命名这个文件夹为myMood

2、创建PHP代码文件
在刚才创建的文件夹里面新建一个php文件,命名为myMood,用来书写插件代码

3、添加头部描述
  1. <?php
  2. /*
  3. Plugin Name: Movie Reviews
  4. Plugin URI: http://wp.tutsplus.com/
  5. Description: Declares a plugin that will create a new post type .
  6. Version: 1.0
  7. Author: Summer
  8. Author URI: http://www.xtwind.com/
  9. License: GPLv2
  10. */
  11. ?>
复制代码
4、注册自定义函数
在刚刚创建的php文件代码中,在?>前面添加函数:
  1. add_action( 'init', 'create_myMood' );
复制代码
得到如下代码:
  1. <?php
  2. /*
  3. Plugin Name: Movie Reviews
  4. Plugin URI: http://wp.tutsplus.com/
  5. Description: Declares a plugin that will create a new post type .
  6. Version: 1.0
  7. Author: Summer
  8. Author URI: http://www.xtwind.com/
  9. License: GPLv2
  10. */
  11. add_action( 'init', 'create_myMood' );
  12. ?>
复制代码
5、添加函数功能
把下面这段代码添加到 add_action( 'init', 'create_myMood' ); 的前面
  1. function create_lsxq() {
  2. register_post_type( 'lsxq',
  3. array(
  4. 'labels' => array(
  5. 'name' => '零散心情',
  6. 'singular_name' => 'lsxq',
  7. 'add_new' => '写心情',
  8. 'add_new_item' => '添加一条新心情',
  9. 'edit' => 'Edit',
  10. 'edit_item' => 'Edit lsxq',
  11. 'new_item' => 'New lsxq',
  12. 'view' => 'View',
  13. 'view_item' => 'View lsxq',
  14. 'search_items' => 'Search lsxq',
  15. 'not_found' => 'No lsxq found',
  16. 'not_found_in_trash' => 'No lsxq found in Trash',
  17. 'parent' => 'Parent lsxq'
  18. ),
  19. 'public' => true,
  20. 'menu_position' => 15,
  21. 'supports' => array( 'title', 'editor', 'comments', 'thumbnail' ),
  22. 'taxonomies' => array( '' ),
  23. 'menu_icon' => plugins_url( 'images/image.png', __FILE__ ),
  24. 'has_archive' => true
  25. )
  26. );
  27. }
复制代码
对 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、创建一个该自定义文章类型的模版
打开刚刚的代码文件,在
  1. add_action( 'init', 'create_lsxq' );
复制代码
语句前面添加下面这一语句:
  1. add_filter( 'template_include', 'include_template_function', 1 );
复制代码
7、实现该函数的功能
  1. function include_template_function( $template_path ) {
  2. if ( get_post_type() == 'lsxq' ) {
  3. if ( is_single() ) {
  4. if ( $theme_file = locate_template( array ( 'single-lsxq.php' ) ) ) {
  5. $template_path = $theme_file;
  6. } else {
  7. $template_path = plugin_dir_path( __FILE__ ) . '/single-lsxq.php';
  8. }
  9. }
  10. }
  11. return $template_path;
  12. }
复制代码
该代码段添加在下面语句的后面
  1. add_filter( 'template_include', 'include_template_function', 1 );
复制代码
8、创建单页面模版single-lsxq.php
创建一个名字为single-lsqx.php的文件,主要代码段如下:
  1. <?php
  2. $mypost = array( 'post_type' => 'lsxq', );
  3. $loop = new WP_Query( $mypost );
  4. ?>
  5. <?php while ( $loop->have_posts() ) : $loop->the_post();?>
  6. <div class="item">
  7. <h3> <span class="fr"><?php the_time('Y-n-j H:i') ?></span> </h3>
  8. <div class="con">
  9. <?php the_content(); ?>
  10. </div>
  11. </div>
  12. <?php endwhile; ?>
复制代码
现在,我们已经通过循环创建了一个基本的页面模板。这个  函数检索自定义文章类型的元素,并在循环中使用它们。现在自定义文章类型差不多就好了,剩下的就是css样式了。

上述代码可点击下方附件下载


myMood.rar (904 Bytes, 下载次数: 0)
回复

使用道具 举报

发布主题
推荐阅读 更多
阅读排行 更多
广告位
全国统一客服电话
400-1234-5678

24x7小时免费咨询

  • 官方在线客服

    QQ客服:小西

    点击交谈

    QQ客服:良子

    点击交谈

    QQ客服:闵月

    点击交谈
  • 安徽省合肥市高新区创新产业园

  • 手机扫码查看手机版

    手机查找资源更方便

  • 扫一扫关注官方微信

    加入官方微信群