代码之家  ›  专栏  ›  技术社区  ›  Balraj

仅当从所选类别发布时才尝试发送推送通知

  •  1
  • Balraj  · 技术社区  · 8 年前

    我试图通过Wordpress插件发送推送通知,但只有在我从给定类别发帖时。但它不起作用。

    /*添加新帖子时处理通知*/

    function fcm_main_transition_post_new($new_status, $old_status, $post){
        $catId = get_cat_ID();
        if( $catId == 'Result'){
            fcm_notif_post_new($new_status, $old_status, $post);
        }
        else{
            echo "This category is not set to send push notification.";
        }
    }
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Argus Duong    8 年前

    实际上是什么 $post 变量如果是 from WordPress core ,使用此代码:

    $categories = get_the_category( $post->ID );
    foreach( $categories as $category ) {
        if( $category->cat_ID == 'Your category ID' ){
            // Send notification please
        }
    
        // If you need category slug check
        if( $category->cat_name == 'Your category slug' ){
            // Send notification please
        }
    }
    

    get_cat_ID() 返回整数,而不是字符串 Result .

    推荐文章