Filter string like a slug

时间:2015-01-09 作者:RoseCoder

我有一个WordPress网站,使用了相当长的链接。我想过滤一个字符串,使其最终的格式与url slug完全相同,有人知道该过滤器或函数吗?

1 个回复
最合适的回答,由SO网友:cybmeta 整理而成

您可以使用sanitize_title() 功能:

$string = "This is title string";
// return "this-is-title-string"
$slug   = sanitize_title( $string );
您还可以过滤sanitize_title() 函数使用sanitize_title filter:

add_filter( \'sanitize_title\' , \'sanitize_filter_callback\', 10, 3 );
function sanitize_filter_callback( $title, $raw_title, $context ) {
    // do something
}

结束

相关推荐

Get multiple tags by slug

以下代码对我来说很好:$term = get_term_by(\'slug\', ‘foo’, \'post_tag\'); $args = array ( \'posts_per_page\' => 5, \'paged\' => $paged, \'post__not_in\' => get_option( \'sticky_posts\' ) , \'tag__not_in\' => $term