【问题标题】:Getting the 'thumbnail ID' from last product category term in WooCommerce从 WooCommerce 中的最后一个产品类别术语获取“缩略图 ID”
【发布时间】:2019-06-21 12:24:17
【问题描述】:

当我在 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
$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_id= get_post_thumbnail_id($lastCategory->id );获取类别缩略图。

之后我使用update_post_meta( $post_id, '_thumbnail_id', $thumbnail_id ); 设置投资组合的缩略图。

但它没有设置任何东西。

我该如何解决?

【问题讨论】:

    标签: php wordpress woocommerce categories taxonomy-terms


    【解决方案1】:

    更新 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-&gt;id 不存在(并输出空值):

    $thumbnail_id= get_post_thumbnail_id($lastCategory->id );
    

    相反,您需要使用 $lastCategory-&gt;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
    

    与产品类别相关的术语:WooCommerce get attribute thumbnail - Variation Swatches and Photos plugin

    【讨论】:

    • 在 phpstorme 中,在 get_woocommerce_term_meta 上画一条线,但它可以工作,我得到缩略图 id,但它没有在投资组合页面上设置
    • 但我的问题是为什么它没有设置?好的,我完成了这个问题并再次问为什么它没有设置。谢谢先生
    • @zeynabfarzaneh 对您的新问题的一些建议,您还应该添加模板 custumcat.php 的相关代码(或指向它的链接)以及有关此模板的更多详细信息。请看:How to create a Minimal, Complete, and Verifiable example(此评论无需回复)
    【解决方案2】:

    首先,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-&gt;id

    【讨论】:

    • 我编辑了我的代码,就像你一样伤心。但它不起作用并且不会添加新类别
    【解决方案3】:

    get_woocommerce_term_meta 自 3.6 版起已弃用 `改为使用 get_term_meta。

    get_term_meta( int $term_id, string $key = '', bool $single = false )
    

    法典reference documentation

    【讨论】:

      猜你喜欢
      • 2021-04-12
      • 1970-01-01
      • 1970-01-01
      • 2015-05-27
      • 2020-12-24
      • 2014-04-06
      • 1970-01-01
      • 2016-01-09
      • 2017-01-10
      相关资源
      最近更新 更多