黄聪:wordpress自定义post_type,并且自定义固定链接

<?
class zsjh
{
    function init()
    {
        add_action( 'init', array($this,'create_zsjh') );
        add_filter('post_type_link', array($this, 'get_permalink') ,1 ,3 );
    }
    
    function create_zsjh()
    {
        $labels = array(
            'name' => '招生计划',   
            'singular_name' => '招生计划',   
            'add_new' => '添加招生计划',   
            'add_new_item' => '添加一个新招生计划',   
            'edit_item' => '编辑招生计划',   
            'new_item' => '新招生计划',   
            'view_item' => '查看',   
            'search_items' => '搜索',   
            'not_found' =>  '还没有添加招生计划哦~',   
            'not_found_in_trash' => '还没有删除的招生计划哦~',    
            'parent_item_colon' => '',  
            'menu_name' => '招生计划',
            'menu_position' => 5
        );   
        $args = array(   
            'labels' => $labels,
            'public' => true,
            'publicly_queryable' => true,
            'show_ui' => true,
            'show_in_menu' => true,
            'query_var' => true,
            'capability_type' => 'post',
            'has_archive' => true,
            'hierarchical' => false,
            'supports' => array('title','editor','thumbnail','comments','custom-fields'),
        );
        
        //自定义一个hc_post_type_zsjh的文章类型
        register_post_type( 'hc_post_type_zsjh', $args);
        //自定义固定链接规则
        add_action('generate_rewrite_rules',array($this, 'jihua_rewrite_rules') );
    }
    
    function jihua_rewrite_rules( $wp_rewrite )
    {
        global $wp_rewrite;
        $wp_rewrite->add_permastruct('hc_post_type_zsjh', '/jihua/%jihua_id%.html', false);
        $new_rules = array('jihua/([0-9]+)?.html$' => 'index.php?post_type=hc_post_type_zsjh&p=$matches[1]' );
        $wp_rewrite->rules = $new_rules + $wp_rewrite->rules; 
    }

    function get_permalink( $permalink, $post, $leavename )
    {
        //替换%jihua_id%为当前文章ID
        $permalink = str_replace("%jihua_id%", $post->ID, $permalink);
        return $permalink;
    }
}

$zsjh = new zsjh();
$zsjh->init();
?>

 

转载于:https://www.cnblogs.com/huangcong/p/4249370.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值