Php 获取';缩略图ID';来自WooCommerce中的上一个产品类别术语
当我在WooCommerce中添加新的类别时,我试图务实地添加新的投资组合 我的代码是:Php 获取';缩略图ID';来自WooCommerce中的上一个产品类别术语,php,wordpress,woocommerce,categories,taxonomy-terms,Php,Wordpress,Woocommerce,Categories,Taxonomy Terms,当我在WooCommerce中添加新的类别时,我试图务实地添加新的投资组合 我的代码是: 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
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);
我想从类别缩略图中设置公文包缩略图,然后
我使用了$thumbnail\u id=get\u post\u thumbnail\u id($lastCategory->id)代码>用于获取类别缩略图
之后,我使用了update_post_meta($post_id,''u thumbnail_id',$thumbnail_id')
设置公文包的缩略图
但它不会设置任何内容。
如何修复它?更新2.1
我一直在测试下面的代码,我得到了正确的$thumbnail\u 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;
这里是DB表“wp_termmeta”的相应屏幕截图:
因此,这是经过测试的,并且有效
这一次,更新发布元数据($post\u id,'u thumbnail\u id',$thumbnail\u id)代码>
将正确设置值
更新1:
产品类别是WordPress自定义分类法,使用WP\u术语
这不起作用,因为$lastCategory->id
不存在(并输出空值):
相反,您需要使用$lastCategory->term\u id
来处理WP\u term
对象,并以这种方式:
$thumbnail_id= get_woocommerce_term_meta( $lastCategory->term_id, 'thumbnail_id', true );
对象属性包括:
term_id
name
slug
term_group
term_taxonomy_id
taxonomy
description
parent
count
与产品类别相关的术语:首先,WooCommerce产品类别是分类法,而不是post
,因此您不能在其上使用函数get\u post\u thumbnail\u id()
。
相反,您可以使用如下内容:
$thumbnail_id = get_woocommerce_term_meta( $term_id, 'thumbnail_id', true );
其次,由于您的以编程方式\u create\u post
函数是create\u product\u cat
的钩子,因此在调用时,它会收到两个参数:$term\u id和$term\u taxonomy\u id。
不需要通过所有这些行查找刚刚创建的产品类别(get_categories()甚至不应该工作,因为您在这里使用的是产品类别,而不是常规的帖子类别):
当您可以像这样简单地声明您的函数时
function programmatically_create_post($term_id, $tt_id) {...}
然后只需使用$term\u id
参数:
$lastCategory = get_term($term_id, 'product_cat');
请确保您也使用$term\u id
而不是$lastCategory->id获取商业\u term\u meta
自3.6版以来已被弃用
`改用get_term_meta
get_term_meta( int $term_id, string $key = '', bool $single = false )
Codex我像你一样编辑我的代码。但它不起作用,也不会在phpstorme drow中添加新的类别,在get_woocommerce_term_meta上有一行,但它起作用,我得到了缩略图id,但它没有在portfolio页面上设置,但我的问题是为什么它没有设置?好的,我完成这个问题,再次询问为什么它没有设置。谢谢Mr@zeynabfarzaneh对于您的新问题,您还应该添加模板custumcat.php
(或其链接)的相关代码,以及有关此模板的更多详细信息。请参阅:…(您无需回答此评论)
$lastCategory = get_term($term_id, 'product_cat');
get_term_meta( int $term_id, string $key = '', bool $single = false )