dqo58772 2014-02-22 20:16
浏览 55
已采纳

无法将Wordpress帖子ID发送到Echo或插入数据库

I've got this code written that when a link is clicked is supposed to insert the current user's id, the current post's id, and the date into a database. The current user id & the current date get inserted into the database and echo's perfectly, but I can't get the current post's id to "echo" or get it to insert into the database. Here's the code I have so far:

global $post;
    global $wpdb;
    $wp_user_id = get_current_user_id(); 
    $activity_post_id = $wp_query->post->ID;
    $activity_added = current_time('mysql', 1);

    echo "<h2>The Current User ID is: ". $wp_user_id . "</h2>";
    echo "<h2>The Current Activity ID is: ". $activity_post_id . "</h2>";
    echo "<h2>The Current Date / Time is: ". $activity_added . "</h2>";

    $wpdb->insert(
        'wp_growwellactivities',
        array(
            'wp_user_id' => $wp_user_id,
            'activity_post_id' => $activity_post_id,
            'date_added' => $activity_added
        )
    ); 
  • 写回答

1条回答 默认 最新

  • dpwbc42604 2014-02-22 20:21
    关注

    You're attempting to access wp_query without using the global.

    Change your globals to:

    global $post, $wp_query, $wpdb;
    

    Or use this instead:

    $activity_post_id = $post->ID;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)