wordpress 限制 post slug 长度

/**
 * 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;
}

你可能感兴趣的:(wordpress 限制 post slug 长度)