wordpress添加自定义文章类型代码(附伪静态url)

作者:admin 发布时间:2018-10-08 16:25:01 浏览:1456次
发现一段非常好玩的代码,如果需要用wordpress添加自定义文章类型,就可以直接使用了,方便简洁。比如添加一个教程文章类型或者产品文章类型等,本代码用于添加一个自定义文章类型,同时设置好伪静态等。可以根据实际需求加以修改。 [cc lang="php"] //添加自定义文章类型代码 new Ws_Post_Type('video','video_list','教程'); Class Ws_Post_Type { private $type,$type_category,$type_name; function __construct($type,$type_category,$type_name) { $this->type = $type; $this->type_category = $type_category; $this->type_name = $type_name; //添加自定义文章类型 add_action('init', array($this,'ws_add_type')); //为商品类自定义类型增加分类功能 add_action('init',array($this,'ws_add_categroy'), 0); //修改自定义类型文章的地址 add_filter('post_type_link',array($this,'ws_add_link'), 1, 3); //让自定义的url支持伪静态 add_action('init', array($this,'ws_rewrites_init')); } //新增自定义文章类型 public function ws_add_type() { register_post_type($this->type, array( 'labels' => array( 'name' =>$this->type_name, 'title' =>$this->type_name, 'singular_name' => '所有'.$this->type_name, 'add_new' => '添加'.$this->type_name, 'add_new_item' => '添加新'.$this->type_name, 'edit' => '编辑', 'edit_item' => '编辑'.$this->type_name, 'new_item' => '新'.$this->type_name, 'view' => '查看'.$this->type_name, 'view_item' => '查看'.$this->type_name, 'search_items' => '搜索'.$this->type_name, 'not_found' => '没有找到相关'.$this->type_name, 'not_found_in_trash' => '没有'.$this->type_name.'评论', 'parent' =>$this->type_name.'评论', ), 'exclude_from_search' => false, 'public' => true, 'menu_position' => 6, 'supports' => array('title', 'editor', 'comments', 'custom-fields', 'thumbnail', 'excerpt', 'page-attributes'), 'taxonomies' => array(''), 'has_archive' => true, 'taxonomies' => array('post_tag'), ) ); } function ws_add_categroy() { register_taxonomy( $this->type_category, $this->type, array( 'labels' => array( 'name' => $this->type_name.'分类', 'add_new_item' => '添加分类', 'new_item_name' => "新'.$this->type_name.'分类" ), 'show_ui' => true, 'show_tagcloud' => true, 'hierarchical' => true, ) ); } function ws_add_link($link, $post = 0) { global $post; if ($post->post_type == $this->type ) { return home_url($this->type . '/' . $post->ID . '.html'); }else{ return $link; } } function ws_rewrites_init() { add_rewrite_rule( $this->type . '/([0-9]+)?.html$', 'index.php?post_type=' . $this->type . '&p=$matches[1]', 'top'); add_rewrite_rule( $this->type . '/([0-9]+)?.html/comment-page-([0-9]{1,})$', 'index.php?post_type=' . $this->type . '&p=$matches[1]&cpage=$matches[2]', 'top'); } } [/cc] 记得,是放在functions.php文件里

如需转载请保留本文出处: https://www.zhe94.com/360.html

上一篇:Best Trace 路由跟踪工具和专业级查ip网站
下一篇:wordpress输出指定类型文章(附调用分类法的某个分类的文章)