• 关于教程
  • 主题制作
  • 读书笔记
  • 生活笔记
  • 关于我
  • 有问题加我QQ 248758228

    自己用的wordpress添加自定义文章类型代码

    更新时间:2018-03-26 15:08:29

    【温馨提示:视频教程将在6月份开始更新,可提前购买,1-10元一节课,联系QQ248758228】


    本代码用于添加一个自定义文章类型,同事设置好伪静态等。可以根据实际需求加以修改。

     

    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');
           }
    }
                   

      0个问题

    WP资料

    工作记录

    生活笔记

    读书笔记

    <