这几天新给博客整加了一个功能历史上的今天功能,目前正在研究如何把它调用,放到首页cms布局指定位置。功能只是非常初级的
什么是 Post Type ?
WordPress 里面内置了两种常用的 Post Type :Post(文章)、Page(页面),分别用来展示两种类型的内容,一种是相对动态的文章,另一种是相对固定的页面。
除此之外,WordPress 还内置了其他几种 Post Type 用来展示不同类型的内容。但如果是一个比较复杂的网站或者需求自定义程度比较高,这时候就可以手动自己创建一个 Post Type 来展示某类信息。
比如大型杂志网站,可以为杂志类型创建个不同的 Post Type 来进行管理;大型企业网站,可以为产品类型创建个不同的 Post Type 来展示。
本教程以本站的历史上的今天为例,介绍如何使用它,
https://www.ljy2345.com/history
创建文章类型
首先我们打开function.php文件
//历史上的今天 function my_custom_post_history() { $labels = array( 'name' => _x( '历史上的今天', 'post type 名称' ), 'singular_name' => _x( '历史上的今天', 'post type 单个 item 时的名称,因为英文有复数' ), 'add_new' => _x( '新建历史', '添加新内容的链接名称' ), 'add_new_item' => __( '新建一个历史' ), 'edit_item' => __( '编辑历史' ), 'new_item' => __( '新历史' ), 'all_items' => __( '所有历史' ), 'view_item' => __( '查看历史' ), 'search_items' => __( '搜索历史' ), 'not_found' => __( '没有找到有关历史' ), 'not_found_in_trash' => __( '回收站里面没有相关历史' ), ); $args = array( 'labels' => $labels, 'description' => '我们的历史信息', 'public' => true, 'menu_position' => 5, 'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ), 'has_archive' => true ); register_post_type( 'history', $args ); } add_action( 'init', 'my_custom_post_history' );
分类功能
如果你要添加分类功能
就需要添加如下字段
register_taxonomy( $taxonomy, $object_type, $args );
这里我们来参考一下网上的代码
function my_taxonomies_movie() { $labels = array( 'name' => _x( '电影分类', 'taxonomy 名称' ), 'singular_name' => _x( '电影分类', 'taxonomy 单数名称' ), 'search_items' => __( '搜索电影分类' ), 'all_items' => __( '所有电影分类' ), 'parent_item' => __( '该电影分类的上级分类' ), 'parent_item_colon' => __( '该电影分类的上级分类:' ), 'edit_item' => __( '编辑电影分类' ), 'update_item' => __( '更新电影分类' ), 'add_new_item' => __( '添加新的电影分类' ), 'new_item_name' => __( '新电影分类' ), 'menu_name' => __( '电影分类' ), ); $args = array( 'labels' => $labels, 'hierarchical' => true, ); register_taxonomy( 'movie_category', 'movie', $args ); } add_action( 'init', 'my_taxonomies_movie', 0 );
我们看到出现了熟悉的文章分类功能,只不过上面的文案全部变成我们自定义的内容了:
还可以填加其他的功能比如meta_box ,因为我不需要这些功能,这里我就不说了,可以参考下面的链接。
https://blog.wpjam.com/article/wordpress-post-type/
指定模板
既然添加了文章类型,我们打开链接到文章页面,就发现里面的模板和single.php是一样的,那我们就需要自定义文章的分类模板和文章模板。
// 指定页面模板 function history_template( $template_path ) { if ( get_post_type() == 'history' ) { if ( is_single() ) { $theme_file = locate_template( array ( 'single-history.php' ) ); $template_path = $theme_file; } elseif ( is_archive() ) { $theme_file = locate_template( array ( 'archive-history.php' ) ); $template_path = $theme_file; } } return $template_path; } add_filter( 'template_include', 'history_template', 1 );
对应的single-history.php就是文章页面,archive-history.php是分类页面模板,只需要在主题根目录建立模板文件即可。
设置固定链接
建立review规则重写链接,默认的链接非常长,看起来很不美观,我就需要用wp_review函数重写链接
//设置history固定链接 function custom_history_link( $link, $post = 0 ){ $history_slug = ot_get_option('history_archives_slug','history'); $product_slug = ot_get_option('history_link_mode')=='post_name'?$post->post_name:$post->ID; if ( $post->post_type == 'history' ){ return home_url( $history_slug.'/good/' . $product_slug .'.html' ); } else { return $link; } } add_filter('post_type_link', 'custom_history_link', 1, 3); function custom_history_rewrites_init(){ $history_slug = ot_get_option('history_archives_slug','history'); if(ot_get_option('history_link_mode')=='post_name'): add_rewrite_rule( $history_slug.'/good/([一-龥a-zA-Z0-9_-]+)?.html([\s\S]*)?$', 'index.php?post_type=history&name=$matches[1]', 'top' ); else: add_rewrite_rule( $history_slug.'/good/([0-9]+)?.html([\s\S]*)?$', 'index.php?post_type=history&p=$matches[1]', 'top' ); endif; } add_action( 'init', 'custom_history_rewrites_init' );
因为我这里是修改了tinection主题后台设置,方便的设置链接是post_name &post_id &链接参数,在别的主题使用就需要修改掉ot_get_option()这个函数即可。
设置固定链接后这个功能就基本完工了,下面就是找一个好看的模板
调用文章
如何调用post_type 的文章
<?php query_posts("post_type=history&post_status=publish&posts_per_page=10");if (have_posts()) : while (have_posts()) : the_post(); ?> <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"<?php the_title(); ?></a></p> <?php endwhile;endif; ?>
posts_per_page是显示几篇文章,orderby = rand是随机 =date 按日期 cat= category_id 指定显示一个分类
如何判断文章类型
<?php if ( get_post_type() == 'movie' ){ }?>
今天就到这了
赞赏历史上的文章
- 2017: MYSQL 批量替换某张表某字段里面的内容( 0)
- 2016: 纯代码实现WordPress自动生成页面静态缓存( 0)
- 2016: 纯代码实现WordPress用户邮箱登录( 0)
除特别注明外,本站所有文章均为LJY IT BLOG原创,转载请注明出处来自https://www.ljy2345.com/2019/02/how-wordpress-customizes-a-post_type-reviwe-rewrites-the-link/
Comments | NOTHING