我想在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的新頁面?
'add_action('create_product_cat','programmatically_create_post',10,2);'試試這個。最後只需要放2個。 –
@MD。 Atiqur拉赫曼感謝它修復 – zfarzaneh