其他分享
首页 > 其他分享> > 自定義wordpress的custom post permalink

自定義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.

标签:permalink,custom,link,wordpress,post,type,id
来源: https://www.cnblogs.com/chenkuang/p/16362288.html