
wordpress如何实现标题自动生成英文拼音,下面web建站小编给大家详细介绍一下2种不同的方法,具体实现代码!
找到主题下functions.php文件,写入以下代码:
方法一:利用百度翻译
function baidu_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 ( emptyempty( $wp_http_get->errors ) ) {
if ( ! emptyempty( $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', 'baidu_slug_auto_translate', 1 );
方法二:利用微软翻译
function baidu_slug_auto_translate( $title ) {
$wp_http_get = wp_safe_remote_get( 'http://api.microsofttranslator.com/v2/ajax.svc/TranslateArray2?appId=***&from="zh"&to="en"&options={}&texts=["' . $title . '"]' );
if ( emptyempty( $wp_http_get->errors ) ) {
if ( ! emptyempty( $wp_http_get['body'] ) ) {
preg_match('/"TranslatedText":"([^"]+)/i', $wp_http_get['body'], $trans_result);
$trans_title = $trans_result[1];
return $trans_title;
}
}
return;
}
根据需求配置wp_http_get
php语法对index.php首页进行判断(根据需要显示不同页面)
wordpress新建页面中页面属性里没有“模板”选项解决方法
标签: wordpress教程, 自动生成拼音
上面是“wordpress如何实现标题自动生成拼音”的全面内容,想了解更多关于 wordpress 内容,请继续关注web建站教程。
当前网址:https://m.ipkd.cn/webs_3509.html
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

关键词索引多少合适?关键词索引对SEO有什么影响?
码云(gitee)提交不显示贡献度解决方法
css3模拟翻书幻灯效果插件
提升百度排名不可忽视的三个方面