为了账号安全,请及时绑定邮箱和手机立即绑定

每当创建另一个自定义帖子类型时创建自定义帖子类型

每当创建另一个自定义帖子类型时创建自定义帖子类型

PHP
幕布斯7119047 2022-01-14 17:05:41
我正在尝试向我的 Wordpress 网站添加一项功能,即每次添加帖子(自定义帖子类型)时,都会创建另一个帖子(不同的自定义帖子类型)。我尝试在我的函数文件中添加一个动作,但它似乎不起作用。函数.phpfunction add_notification($post_id) {    global $wpdb;    $post_type = get_post_type($post_id);    if($post_type == 'exercises'){        $title = "BLA";        $post_id_new = wp_insert_post(            array(                'comment_status'    =>  'closed',                'ping_status'       =>  'closed',                'post_title'        =>  $title,                'post_status'       =>  'publish',                'post_type'     =>  'notification'            )        );    }}add_action('publish_post', 'add_notification');我也尝试过使用其他钩子,如 new_to_publish 和 publish_post有了这个,当我添加一个练习帖子时,我期待有一个新帖子(通知)。我认为这是我忘记的愚蠢的事情,但我已经陷入了这个问题已经有 3 天了。
查看完整描述

1 回答

?
饮歌长啸

TA贡献1951条经验 获得超3个赞

对于自定义帖子类型,我们必须使用像 add_action('publish_custom-post-name','function'); 这样的钩子。在您的情况下,您必须使用如下所示


function add_notification($post_id) {

    global $wpdb;

    $post_type = get_post_type($post_id);

    if($post_type == 'exercises'){

        $title = "BLA";

        $post_id_new = wp_insert_post(

            array(

                'comment_status'    =>  'closed',

                'ping_status'       =>  'closed',

                'post_title'        =>  $title,

                'post_status'       =>  'publish',

                'post_type'     =>  'notification'

            )

        );

    }

}

add_action('publish_exercises', 'add_notification');

请试试这个。如果有任何疑问,请随时询问。


查看完整回答
反对 回复 2022-01-14
  • 1 回答
  • 0 关注
  • 131 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号