将代码加到主题functions.php即可,指点聚主题的客户请将代码加入到uctheme.php即可。
- function uctheme_slug_auto_translate( $title )
- {
- $wp_http_get = wp_safe_remote_get( ‘http://fanyi.baidu.com/v2transapi?from=zh&to=en&transtype=trans&query=’ . $title );
- if ( empty( $wp_http_get->errors ) )
- {
- if ( ! empty( $wp_http_get[‘body’] ) )
- {
- $trans_result = json_decode( $wp_http_get[‘body’], true );
- $trans_title = $trans_result[‘trans_result’][‘data’][0][‘dst’];
- return $trans_title;
- }
- }
- return;
- } add_filter( ‘sanitize_title’, ‘uctheme_slug_auto_translate’, 1 );
原理就是通过百度翻译的接口,自动翻译你填写的文章名,不过看着如果文章名长的话,别名也很长啊