科技美学

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  90 随笔 :: 0 文章 :: 1 评论 :: 69570 阅读

1. wordpress hook in construct()

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

  

2. rewrite post link

1
2
3
4
5
6
7
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

1
2
3
4
5
6
7
8
9
10
11
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   chankuang  阅读(94)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示