自定義wordpress的custom post permalink

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.

相关文章

我想将wordpress的默认接口路由改掉,愿意是默认的带一个 wp...
wordpress自定义分类法之后,我看到链接都自动在后面添加了一...
事情是这样的,我用 get_post_type 函数创建了一个自定义分类...
最近网站莫名其妙的被顶上了,过一个多小时,就注册一个账号...
最近服务器要到期了,就想着把网站转移到另外一台服务器,本...
今天在写wordpress的接口,然后碰到个奇怪的问题,怎么访问都...