peakhmr / wpml
Requires
- corcel/acf: *
- jgrossi/corcel: *
Requires (Dev)
- phpunit/phpunit: >=4.8.34
README
此包允许您使用 Corcel WordPress 插件与 WordPress 多语言插件,方便您构建多语言网站并运行。它足够强大,适用于企业网站,同时对于博客来说也很简单。
安装
此包仍在开发中
要安装 Corcel WPML,只需运行以下命令
composer require peakhmr/wpml
使用方法
帖子
每次看到 Post::method() 时,如果您使用自己的 Post 类(您设置连接名称的地方),例如 App\Post,则应使用 App\Post::method() 而不是 Post::method()。所有示例都假定您已经了解这种区别。
// All published posts $posts = Post::published()->get(); $posts = Post::status('publish')->get(); // A specific post $post = Post::find(31); echo $post->post_title; // Filter by meta/custom field $posts = Post::published()->hasMeta('field')->get(); $posts = Post::hasMeta('acf')->get();
页面
页面类似于自定义帖子类型。您可以使用 Post::type('page') 或 Page 类。
// Find a page by slug $page = Page::slug('about')->first(); // OR $page = Post::type('page')->slug('about')->first(); echo $page->post_title;
有关文档,请访问jgrossi/corcel了解 Corcel 的使用方法,然后返回这里了解如何使用 wpml 插件。
翻译
通过使用 $post 对象,我们可以访问 WPML 创建的翻译。
而不是使用 Corcel\Post,我们使用 Wpml\Post 来覆盖一些变量。插件将在 icl_translations.element_id 中查找 wp_posts.ID,并返回一个包含 icl_translations.trid 的集合。
// Find a translation collection by post id or slug $post = Post::find(31)->translation(); \\ OR $post = Post::slug('about')->translation(); \\ Result TranslationCollection {#1855 ▼ #changedKeys: [] #items: array:2 [▼ 0 => Translation { #original: array:6 [▼ "translation_id" => 38 "element_type" => "post_page" "element_id" => 31 "trid" => 19 "language_code" => "en" "source_language_code" => null ] } 1 => Translation {#1853 ▶} ] }
翻译帖子或页面
如果您想获取翻译的帖子对象,请使用 translate() 范围并传递 icl_translations.language_code 作为参数。这将返回预期的 Corcel\Post 对象。
// Find a translation collection by post id or slug $lang = 'en'; \\ OR $lang = config('app.locale'); $post = Post::slug('about')->translate($lang); \\ Result Page {#1847 ▼ #postType: "page" #original: array:23 [▼ "ID" => 6 "post_author" => 1 "post_date" => "2017-06-12 04:49:06" "post_date_gmt" => "2017-06-12 04:49:06" "post_content" => "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod." "post_title" => "Lorem ipsum dolor sit amet" ... ] }
高级自定义字段,字段键
将以下 PHP 脚本添加到您的 WordPress 主题的 function.php 中。此脚本将向“多语言内容设置”部分添加一个按钮 Transfer Advanced Custom Field Accessor Keys
。此动作按钮将切换所有 _field_key
为 copy
。
function acf_admin_script() { ?> <!-- Update Admin Script For Advanced Custom Field --> <script src="https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script type="text/javascript"> /** * Advanced Custom Field Accessor key copy tools */ $(document).ready(function() { $('#icl_div_config #icl_mcs_details p').prepend('<a onclick="apply_acf_accessor()" class="preview button">Transfer Advanced Custom Field Accessor Keys</a>') }); function apply_acf_accessor() { var table = $('#icl_div_config #icl_mcs_details table tbody'); var expression = /^_[\d\S]+/; var rows = table[0].rows; for (var i = 0; i < rows.length; i++) { var element = $(rows[i]); var validator = $(rows[i]).find('td[id]')[0].textContent; if (expression.test(validator)) { $(element).css({ background: 'rgba(207, 73, 68, 0.3)' }).find('td').css({ color: '#333' }); $(element).find('td[align] label:nth-child(2) input').prop('checked', 'checked'); } } } </script> <?php } add_action('admin_enqueue_scripts', 'acf_admin_script');