当用户单击“为我的自定义帖子类型包添加新类别”时,我希望同时创建一个与新类别同名的新页面。我可以通过代码创建一个新页面,但我找不到适合我的函数的钩子。php来运行它。
我需要的是一个钩子,它将只运行于包的post类型。
谢谢
当用户单击“为我的自定义帖子类型包添加新类别”时,我希望同时创建一个与新类别同名的新页面。我可以通过代码创建一个新页面,但我找不到适合我的函数的钩子。php来运行它。
我需要的是一个钩子,它将只运行于包的post类型。
谢谢
您可以尝试created_term
挂钩:
/**
* Do \'stuff\' when a term is created in the \'country\' taxonomy
* using info from the referred page
*
* @param integer $term_id
* @param integer $tt_id
* @param string $taxonomy
* @return void
*/
function custom_created_term( $term_id, $tt_id, $taxonomy )
{
$my_cpt = \'packages\'; // Edit this to your needs
$my_tax = \'category\'; // Edit this to your needs
if( DOING_AJAX && $my_tax === $taxonomy )
{
// Try to get the post type from the post id in the referred page url
// Example: /wp-admin/post.php?post=2122&action=edit&message=1
parse_str( parse_url( wp_get_referer(), PHP_URL_QUERY ) , $params );
if( isset( $params[\'post\'] ) )
{
$post_id = intval( $params[\'post\'] );
if( $post_id > 0 && $my_cpt === get_post_type( $post_id ) )
{
// do stuff ...
}
}
}
}
ajax请求发送到admin-ajax.php
使用“+添加新类别”在编辑帖子屏幕中创建新类别时。此ajax请求中发送的表单数据似乎不包含关于当前post对象的任何显式信息。因此,我使用引用的页面信息作为最后手段。也许还有其他更好的方法可以做到这一点,但我不确定如何做到;-)
我认为使用WordPress钩子进行自定义分类将更简洁、更快。
do_action( "create_{$taxonomy}", int $term_id, int $tt_id )
例如,在创建新的WooCommerce类别时启动函数:function custom_created_term( $term_id, $tt_id ) {
// do stuff
}
add_action(\'create_product_cat\',\'custom_created_term\');
我创建了一个自定义小部件,它应该显示一个包含博客所有类别的选择菜单。我使用get\\u categories来编译列表。这很好,所有类别都显示在下拉菜单中。每次我保存并刷新小部件页面时,自定义小部件就不再存在了。我检查过了function update 那里一切都很好。所以我想这一定是我创建表单的方式。有什么想法吗?提前谢谢。我不想转储所有代码,所以我只粘贴了创建表单的函数。如果你需要更多,请发表评论function form( $instance ) { /* Default