(更新)使用 WPML 获取第二语言的 WooCommerce 类别
(Updated) Get WooCommerce category on second language using WPML
我在我的 WordPress 网站上使用西班牙语和英语的 WPML。
我需要从翻译的(英语)类别中检索 WooCommerce 产品。这是我的主要语言(西班牙语)的工作代码:
// get all products from the category talleres
$productos = wc_get_products([
'status' => 'publish',
'limit' => -1,
'product_cat' => 'talleres'
]);
// then get some information about each product in that category
$locaciones = array_map(function ($product) {
/** @var WC_Product $product */
return [
'lat' => get_post_meta( $product->get_id(), 'lat_mapa_tl', true),
'long' => get_post_meta( $product->get_id(), 'long_mapa_tl', true),
'icono' => get_post_meta( $product->get_id(), 'icono_mapa_tl', true ),
'popup_html' => '<div class="titulo">' . $product->get_title() . '</div><br /><a href="' . get_permalink( $product->get_id() ) . '" class="sinborde"><img src="' . get_the_post_thumbnail_url( $product->get_id(), 'full' ) . '" width="250" height="141" title="Hacé click para ver el time-lapse" /></a><br /><div class="coleccion">Categorizado en ' . $product->get_categories() . '</div><a href="' . get_permalink( $product->get_id() ) . '">Ver time-lapse</a>'
];
}, $productos);
一切正常。当尝试使用 talleres 的翻译类别时,问题就来了,即 "workshops"。使用该类别,这将是我的代码:
$productos = wc_get_products([
'status' => 'publish',
'limit' => -1,
'product_cat' => 'workshops'
]);
但是那是从原始的未翻译的西班牙语类别中获取信息,而不是英语(我的第二语言)。
编辑:
有人建议我应该在 wc_get_products
:
中添加这一行
'suppress_filters' => true
但这没有用。
我找到了解决问题的方法 here。刚刚添加了这两行代码,一切都按预期工作:
global $sitepress;
// switch to the desired language, French for example
$sitepress->switch_lang( 'fr' );
编辑:这是另一个可能的修复方法,以防其他人需要它:https://wpml.org/forums/topic/problems-to-show-in-all-languages-suppress_filters-true-not-working/#post-872537
没有测试。
我在我的 WordPress 网站上使用西班牙语和英语的 WPML。
我需要从翻译的(英语)类别中检索 WooCommerce 产品。这是我的主要语言(西班牙语)的工作代码:
// get all products from the category talleres
$productos = wc_get_products([
'status' => 'publish',
'limit' => -1,
'product_cat' => 'talleres'
]);
// then get some information about each product in that category
$locaciones = array_map(function ($product) {
/** @var WC_Product $product */
return [
'lat' => get_post_meta( $product->get_id(), 'lat_mapa_tl', true),
'long' => get_post_meta( $product->get_id(), 'long_mapa_tl', true),
'icono' => get_post_meta( $product->get_id(), 'icono_mapa_tl', true ),
'popup_html' => '<div class="titulo">' . $product->get_title() . '</div><br /><a href="' . get_permalink( $product->get_id() ) . '" class="sinborde"><img src="' . get_the_post_thumbnail_url( $product->get_id(), 'full' ) . '" width="250" height="141" title="Hacé click para ver el time-lapse" /></a><br /><div class="coleccion">Categorizado en ' . $product->get_categories() . '</div><a href="' . get_permalink( $product->get_id() ) . '">Ver time-lapse</a>'
];
}, $productos);
一切正常。当尝试使用 talleres 的翻译类别时,问题就来了,即 "workshops"。使用该类别,这将是我的代码:
$productos = wc_get_products([
'status' => 'publish',
'limit' => -1,
'product_cat' => 'workshops'
]);
但是那是从原始的未翻译的西班牙语类别中获取信息,而不是英语(我的第二语言)。
编辑:
有人建议我应该在 wc_get_products
:
'suppress_filters' => true
但这没有用。
我找到了解决问题的方法 here。刚刚添加了这两行代码,一切都按预期工作:
global $sitepress;
// switch to the desired language, French for example
$sitepress->switch_lang( 'fr' );
编辑:这是另一个可能的修复方法,以防其他人需要它:https://wpml.org/forums/topic/problems-to-show-in-all-languages-suppress_filters-true-not-working/#post-872537
没有测试。