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 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?