I want to write some simple code to figure out if the user is the author of any posts (Not if their role is author)

我想写一些简单的代码来确定用户是否是任何帖子的作者(如果他们的角色是作者,则不是)

My attempt is as follows but I can't figure it out

我的尝试如下,但我无法弄清楚

  • I got the userID (set it to current in global header)
  • 我得到了userID(在全局标题中将其设置为当前)

  • I have created a query to for custom-post-type and author
  • 我已经为自定义后期类型和作者创建了一个查询

  • I have got all the current posts from the query
  • 我从查询中获得了所有当前帖子

  • I have added an IF statement (Which I CANT seem to get right)
  • 我添加了一个IF语句(我似乎无法做到)

Here is my code try

这是我的代码试试

<?php
// Global variable for current user ID
$user_ID;

// Create a new WP query object           
$my_query = new WP_Query( array(
'post_type' => 'farmers',
'author' => $user_ID
));

// Get all the current user posts from the query object
$posts = $my_query->posts;

IF There are Posts (Not sure how to write this line) {
// Do something
} else {
// Do Something else
} ?>

Thanks for any help guidance

感谢您的任何帮助指导

1 个解决方案

#1


2

How about:

if( $posts ) {
  // Do something
} 
else {
  // Do Something else
}

更多相关文章

  1. php mail函数一段好的代码
  2. 用于上传多个文件的PHP代码
  3. (phpQuery)对网站产品信息采集代码的优化
  4. 韩顺平_php从入门到精通_视频教程_学习笔记_源代码图解_PPT文档
  5. 【MySQL 技巧分享】 mysql -e 加 v 简化代码
  6. Oracle相当于MySQL代码“插入虚拟”以返回错误消息
  7. 登录使用PHP并´t显示任何html代码
  8. 如果有多个作者[重复],我怎么能阻止PHP显示该书两次
  9. 在代码点火器中从mysql迁移到postgresql

随机推荐

  1. 在android中玩转wcf
  2. Android入门教程(九)之-----取得手机屏幕
  3. android 监听电源键
  4. android 监听webview的超链接点击
  5. Android获取应用程序信息——PackageMana
  6. 銆婄涓€琛屼唬鐮丄ndroid銆嬬瑪璁?/h1
  7. Android(安卓)ContentResolver CallLog
  8. 《Android高级进阶》— Android 书籍
  9. 安卓基础学习 Day01 |第一个安卓应用程序
  10. Android应用程序发送广播(sendBroadcast)的