2017-02-11 61 views
0

我想在WooCommerce中添加新類別時自動創建新頁面。新頁面塊與新類別相同。在woocammerce中添加新類別時創建頁面

所以我的代碼是:

function programmatically_create_post() { 

$author_id = 1; 
$taxonomy = 'product_cat'; 
$orderby = 'name'; 
$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=$all_categories[0]; 
$slug =$lastCategory->slug; 
$title=$lastCategory->name; 
// 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' => 'page', 
     'post_slug'=> $slug 

) 

); 



// 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, 1); 

我想獲取數據庫的最後一類,但它是非常wrong.I谷歌上搜索,但我無法找到我的問題。

當我添加新類別時,如何獲得新的類別slu??

有沒有任何野獸的方式來添加新的類別相同slug的新頁面?

+0

'add_action('create_product_cat','programmatically_create_post',10,2);'試試這個。最後只需要放2個。 –

+0

@MD。 Atiqur拉赫曼感謝它修復 – zfarzaneh

回答

0

你的整個代碼都很好,但你只有一點小小的優先問題。現在,你只需要1的優先級更改爲2

add_action('create_product_cat', 'programmatically_create_post', 10, 2);

這應該工作!

+0

你能給我create_product_cat教程的網址嗎? – zfarzaneh

+0

我會推薦WordPress codex。瞭解掛鉤以及如何進行過濾。 –

相關問題