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

  • 相关阅读:
    自建mail服务器之一:dns解析
    区间树
    3d tech
    3d
    平板比较
    Node。js 访问gmail
    node nightmare 网页自动化测试 sample
    node start
    中國駐香港外交部
    create a simple COM object
  • 原文地址:https://www.cnblogs.com/chenkuang/p/16362288.html
Copyright © 2020-2023  润新知