我今天开始学习挂钩,并尝试完成以下内容。
当我的网站主页加载时,它会在我的自定义分类法中插入一个新术语,名为amenities
. 然后使用动态挂钩(create_{$taxonomy}
, 对我来说是的create_amenities
) 并借助连接到create_amenities
我正在打印新添加的term_id
.
这就是我所做的
1. Created a function to print the new term_id, which accepts one parameter and hooked it into above.
add_action(\'create_amenities\', \'tourplan_print_new_amenity_term_id\', 10, 1);
function tourplan_print_new_amenity_term_id($term_id) {
// Printing twice while should only print once.
echo \'The newly added term id is: \'. $term_id;
}
2. Inside my index.php I have written the following:
$arr_term = array(
\'description\' => \'Honeymoon Suit for couple\',
\'slug\' => \'honeymoon-suite\'
)
$new_term = $wp_insert_term(\'Honeymoon Suit\', \'amenities\', $arr_term);
$new_term_id = $new_term[\'term_id\'];
do_action(\'create_amenities\', $new_term_id);
3. And finally I am removing the term from taxonomy after calling above do_action
method:
wp_remove_term($new_term_id, \'amenities\');
除了钩子函数将ID的值打印两次外,其他一切都正常。我做错了什么?
PS:我没有使用任何现成的主题或任何子主题。这是一个完全手工制作的主题。