wordpress 限制 post slug 长度

来源:互联网 发布:淘宝电子烟哪个牌子好 编辑:程序博客网 时间:2024/04/30 00:02
/** * limit slug length 100 characters. */add_filter( 'wp_unique_post_slug', 'custom_unique_post_slug', 10, 4 );function custom_unique_post_slug( $slug, $post_ID, $post_status, $post_type) {if ( 'post' == $post_type ) {$post = get_post($post_ID);if ( empty($post->post_name) || $slug != $post->post_name ) {if(strlen($slug)>100){if(substr($slug,99,100)=='-'){$slug = substr($slug, 0,99);}else{$slug = substr($slug, 0,100);}//$slug = wp_unique_post_slug($slug, $post_ID, $post_status, $post_type, '0');global $wpdb;$sql = "SELECT count(ID) FROM $wpdb->posts WHERE post_name like '".$slug."%' AND ID!=".$post_ID;$num = $wpdb->get_var($sql);if($num){$slug = $slug.'-'.($num+1);}}}}return $slug;}

0 0