科技美学

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1. wordpress hook in construct()

function __construct() {
  add_filter('post_type_link', [$this, 'custom_nft_permalink'], 1, 3);
}

  

2. rewrite post link

public function initialize() {
  $args = array(
    'rewrite' => array( 'slug' => 'custom_link/?id=%ID%/', 'with_front' => false ),
  )
}

register_post_type( 'custom_post', $args );

  

3. trigger hook func to modify permalink

function custom_nft_permalink($post_link, $id = 0, $leavename) {
  if ( strpos('%ID%', $post_link) === 'FALSE' ) {
    return $post_link;
  }
  $post = &get_post($id);
  if ( is_wp_error($post) || $post->post_type != 'custom_post' ) {
    return $post_link;
  }
  $post_link = substr($post_link, 0, strpos($post_link, "%ID%"));
  return $post_link.''.$post->ID;
}

  

4. Complete!

 

 The permalink will be changed.

posted on 2022-06-10 10:23  chankuang  阅读(94)  评论(0编辑  收藏  举报