我正在WordPress中使用插件WordPress,并尝试执行以下操作:
目标避免了Polylang在使用Polylang复制功能复制帖子时创建类别翻译的默认行为。只有已经有翻译的类别才应转到重复的员额。
--到目前为止,我是如何在函数中使用pll_copy_taxonomies()来激发代码,并使用pll_get_term()来识别哪些类别有翻译,最后只使用wp_set_object_terms()传递那些翻译(我还尝试了在这里产生相同效果的wp_set_post_terms()和wp_set_post_categories() )。
问题因此,代码只传输现有的类别,而不创建新的翻译。但是,当我有一个重复的帖子,我最终删除原来的帖子,原来的类别,没有任何翻译将开始重复自己的数百人。每一个复制的类别都会有像exampleslug-1,exampleslug-2,exampleslug-3这样的片段.例子-176.
看起来Polylang以一种不同的方式处理类别,当我不使用Polylang函数时,它会创建这个bug,我无法理解。
add_filter('pll_copy_taxonomies', 'donotsync', 10, 5);
function donotsync($taxonomies, $sync, $from, $to, $lang) {
//Bail early if not WP built-in posts
if ('post' !== get_post_type($from)) {
return $taxonomies;
}
//*** Get the post categories and transfer only the ones already translated
//*** PROBLEM: This seems to work, but when the article with the default language gets eventually deleted, WP starts duplicating hundreds of copys of the categories from that post that don't have translations yet. Without this block of code, the categories aren't transferred and there's no error.
$post_categories = get_the_category($from);
foreach ($post_categories as $key => $terms) {
$category_id = $terms->term_id;
//Check if Polylang function exists
if (function_exists('pll_get_term')) {
//Check if translation for the category already exists and than transfer it to the duplicated post
$term_translation = pll_get_term($category_id, $lang);
if (!empty($term_translation)) {
wp_set_object_terms($to, $term_translation, 'category', true);
}
}
}
//Remove taxonomy sync
unset($taxonomies['category']);
return $taxonomies;
}发布于 2022-07-22 14:55:01
其实我自己找到了解决办法。问题是,我没有使用来自$taxonomies过滤器的给定pll_copy_taxonomies来创建循环。看着插件文件,我意识到它是怎么做的。
如果翻译的话,我还添加了用于转移Yoast SEO主类别的内容:
if (function_exists('yoast_get_primary_term_id')) {$primary_category = yoast_get_primary_term_id('category', $from);}
if ($primary_category == $term){update_post_meta($to,'_yoast_wpseo_primary_category',$term_translation);}
并且如果翻译了每个标记,也要转移它:
//Get the tag terms
foreach ($taxonomies as $tax) {
if ($tax == 'post_tag') {
if ($terms = get_the_terms($from, $tax)) {
$terms = array_map('intval', wp_list_pluck($terms, 'term_id'));
foreach ($terms as $term) {
//Check if Polylang function exists
if (function_exists('pll_get_term')) {
//Filter terms with translations only
if ($term_translation = pll_get_term($term, $lang)) {
//Transfer only terms with translations
wp_set_object_terms($to, $term_translation, $tax, true);
}
}
}
}下面的是完整的工作代码
add_filter('pll_copy_taxonomies', 'donotsync', 10, 5);
function donotsync($taxonomies, $sync, $from, $to, $lang) {
//Bail early if not WP built-in posts
if ('post' !== get_post_type($from)) {
return $taxonomies;
}
//Check if primary category is set using Yoast SEO
if (function_exists('yoast_get_primary_term_id')) {$primary_category = yoast_get_primary_term_id('category', $from);}
//Get the category terms
foreach ($taxonomies as $tax) {
if ($tax == 'category') {
if ($terms = get_the_terms($from, $tax)) {
$terms = array_map('intval', wp_list_pluck($terms, 'term_id'));
foreach ($terms as $term) {
//Check if Polylang function exists
if (function_exists('pll_get_term')) {
//Filter terms with translations only
if ($term_translation = pll_get_term($term, $lang)) {
//Transfer only terms with translations
wp_set_object_terms($to, $term_translation, $tax, true);
//Transfer the primary category if it is translated
if ($primary_category == $term){update_post_meta($to,'_yoast_wpseo_primary_category',$term_translation);}
}
}
}
}
}
}
//Get the tag terms
foreach ($taxonomies as $tax) {
if ($tax == 'post_tag') {
if ($terms = get_the_terms($from, $tax)) {
$terms = array_map('intval', wp_list_pluck($terms, 'term_id'));
foreach ($terms as $term) {
//Check if Polylang function exists
if (function_exists('pll_get_term')) {
//Filter terms with translations only
if ($term_translation = pll_get_term($term, $lang)) {
//Transfer only terms with translations
wp_set_object_terms($to, $term_translation, $tax, true);
}
}
}
}
}
}
//Remove taxonomy sync
unset($taxonomies['category']);
unset($taxonomies['post_tag']);
return $taxonomies;
}https://stackoverflow.com/questions/73064842
复制相似问题