获取WooCommerce中上一个产品类别术语中的'缩略图ID'
我正在尝试实用添加新投资组合当我在WooCommerce中添加新类别时。获取WooCommerce中上一个产品类别术语中的'缩略图ID'
我的代码是:
function programmatically_create_post() {
$author_id = 1;
$taxonomy = 'product_cat';
$orderby = 'id';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$all_categories = get_categories($args);
$lastCategory=end($all_categories);
$slug =$lastCategory->slug;
$title=$lastCategory->name;
$thumbnail_id= get_post_thumbnail_id($lastCategory->id);
// If the page doesn't already exist, then create it
if(null == get_page_by_title($title)) {
// Set the post ID so that we know the post was created successfully
$post_id = wp_insert_post(
array(
'post_author' => $author_id,
'post_name' => $slug,
'post_title' => $title,
'post_status' => 'publish',
'post_type' => 'us_portfolio',
'post_parent' =>11,
'page_template' =>'custumcat.php',
'post_slug'=> $slug
)
);
update_post_meta($post_id, '_wp_page_template', 'custumcat.php');
update_post_meta($post_id, '_thumbnail_id', $thumbnail_id);
// Otherwise, we'll stop
} else {
// Arbitrarily use -2 to indicate that the page with the title already exists
$post_id = -2;
} // end if
} // end programmatically_create_post
add_action('create_product_cat', 'programmatically_create_post', 10,2);
我想从类别缩略图一套组合缩略图, 我为GET类别缩略图使用$thumbnail_id= get_post_thumbnail_id($lastCategory->id);
。
之后我用update_post_meta($post_id, '_thumbnail_id', $thumbnail_id);
设置投资组合的缩略图。
但它没有设置任何东西。
我该如何解决?
更新2.1
我一直在测试下面的代码,我和得到了正确的$thumbnail_id
没有错误:
$categories = get_categories(array(
'taxonomy' => 'product_cat',
'orderby' => 'id',
'show_count' => 0,
'pad_counts' => 0,
'hierarchical' => 1,
'title_li' => '',
'hide_empty' => 0
));
$last_cat = end($categories); // last category
$last_cat_term_id = $last_cat->term_id; // Value is
$thumbnail_id = get_woocommerce_term_meta($last_cat_term_id, 'thumbnail_id', true);
echo 'Term ID is: ' . $last_cat_term_id . '<br>';
echo 'Thumbnail ID is: ' . $thumbnail_id;
它显示最后一个类别(与此数据相关到我的产品类别设置):
Term ID is: 48
Thumbnail ID is: 443
而这里DB表 “wp_termmeta” 对应的截图:
所以这是测试和工程。
这一次,
update_post_meta($post_id, '_thumbnail_id', $thumbnail_id);
会正确设置一个值。
...
更新1:
产品类别是使用WP_terms
一个WordPress自定义分类...
这不起作用,因为$lastCategory->id
没有按不存在(和输出一个空值):
$thumbnail_id= get_post_thumbnail_id($lastCategory->id);
相反,你需要使用$lastCategory->term_id
将与WP_Term
对象和get_woocommerce_term_meta()
以这种方式工作:
$thumbnail_id= get_woocommerce_term_meta($lastCategory->term_id, 'thumbnail_id', true);
的WP_Term
对象属性:
term_id
name
slug
term_group
term_taxonomy_id
taxonomy
description
parent
count
R兴高采烈的产品类别项:WooCommerce get attribute thumbnail - Variation Swatches and Photos plugin
首先,一个WooCommerce产品类别是taxonomy
,而不是一个post
,所以你不能使用该功能get_post_thumbnail_id()
就可以了。 相反,你可以使用这样的事情:
$thumbnail_id = get_woocommerce_term_meta($term_id, 'thumbnail_id', true);
其次,由于你的programmatically_create_post
功能是create_product_cat
钩,调用时,它接收到2个参数:$ term_id和$ term_taxonomy_id。 没有必要通过所有这些行来查找刚刚创建的产品类别(get_categories()甚至不应该工作,因为你的产品类别在这里工作,而不是常规的岗位类别):
$all_categories = get_categories($args);
$lastCategory=end($all_categories);
当你可以简单地声明你的函数一样
function programmatically_create_post($term_id, $tt_id) {...}
然后简单地利用$term_id
参数:
$lastCategory = get_term($term_id, 'product_cat');
确保你一个请使用$term_id
而不是$lastCategory->id
。
我编辑了我的代码你难过 。但它不起作用,不会添加新的类别 –
请发布您的更新代码... –
我编辑它,但它不修复 –
get_woocommerce_term_meta获取错误。 –
phpstorme在get_woocommerce_term_meta上打了一条线,但它工作,我得到缩略图ID,但它没有设置在投资组合页面 –