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中is_singular()函数简介

函数is_singular()检测后果为true,当上面其一前往true时:is_single()、is_page()、is_attachment()。 假如...

让WordPress媒体库辨认.pdf文件的办法

让媒体库支持pdf分类 这段来自tutsplus的代码能够协助咱们完成如上图所示的成果,将代码放到主题的function...

利用phpmyadmin找回wordpress遗记治理明码

1、确认你的数据库名通常有多个数据库或许一个数据库里装置了多套顺序,所以预备工作就是确认你的数据库名和...

去除wordpress中评论中的nofollow的办法

咱们先来看一下什么是nofollow。NoFollow是Google几年前提出的一个新标签,目的是缩小渣滓留言。此标签标明...

优化WordPress的Google字体以减速国际效劳器上的运转

因为Google被彻底墙掉,不翻墙就无奈加载谷歌字体。很多老手冤家常常埋怨本人买的主机如何如何慢,给的示例...

WordPress的6种主题框架比照剖析

这些规范化代码蕴含了一些根本的构造框架,你只要按本人的需求填补这个框架,可能的话需求调整某些配置。 不...

需求提交

客服服务

亿鸽在线客服系统