WordPress使用自定义文章类型实现任意模板的方法


当前第2页 返回上一页

对 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样式了。

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

希望本文所述对大家基于wordpress的网站建设有所帮助。


标签:WordPress

返回前面的内容

相关阅读 >>

wordpress中is_sticky()判断文章是否置顶的参数与用法

wordpress数据库优化和清理冗余数据的方法

wordpress升级版本及安装插件出现”problem with the ssl ca cert”的解决办法

wordpress随机调用显示文章的方法总结

wordpress判断用户是否登录的代码

菜鸟使用wordpress建站的几点心得

优化wordpress分类链接及wp-no-category-base的卸载方法

wordpress实现获取父类分类名称的方法

wordpress开发之插件开发初识(wordpress插件开发基础)

在coreos上搭建一个wordpress程序操作实例

更多相关阅读请进入《wordpress》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...