本文关键详细介绍了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 这一涵数传出申明,它就为新的文章类型搞好了各种各样管理方法作用。这一涵数包含2个主要参数:第一个是界定了自定义文章类型的姓名 ;第二个是一个数组,用于重新定义的自定义文章类型的特性。
第一个主要参数非常简单,大伙儿自身理解。这儿简易说下影响力个主要参数:
‘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样式了。
所述编码可点一下这里本网站免费下载。
期待文中上述对大伙儿根据wordpress的企业网站建设有一定的协助。