2017-07-24 115 views
1

我正在嘗試實用添加新投資組合當我在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);設置投資組合的縮略圖。

但它沒有設置任何東西。

我該如何解決?

回答

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」 對應的截圖:

// Displays 443 (the correct 'thumbnail_id'set in DB table "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

+0

我編輯它,但它不修復 –

+0

get_woocommerce_term_meta獲取錯誤。 –

+0

phpstorme在get_woocommerce_term_meta上打了一條線,但它工作,我得到縮略圖ID,但它沒有設置在投資組合頁面 –

0

首先,一個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

+0

我編輯了我的代碼你難過 。但它不起作用,不會添加新的類別 –

+0

請發佈您的更新代碼... –

相關問題