Php 在页面加载时获取wordpress post/page ID并将其存储在变量中

Php 在页面加载时获取wordpress post/page ID并将其存储在变量中,php,wordpress,Php,Wordpress,如何检索Post或Page ID并将其存储在变量中,以便在处理数据的短代码中使用 我有一个处理下载按钮的功能,并希望将下载处理的页面/帖子ID与时间戳一起存储,以便跟踪下载的页面/帖子 这是当前正在存储的数据,我还想添加post_ID()数据 $data_id = $wpdb->get_row("select * from ahm_files where id='$id'",ARRAY_A); $wpdb->insert('wp_ahm_download_stats',array(

如何检索Post或Page ID并将其存储在变量中,以便在处理数据的短代码中使用

我有一个处理下载按钮的功能,并希望将下载处理的页面/帖子ID与时间戳一起存储,以便跟踪下载的页面/帖子

这是当前正在存储的数据,我还想添加post_ID()数据

$data_id = $wpdb->get_row("select * from ahm_files where id='$id'",ARRAY_A); 
$wpdb->insert('wp_ahm_download_stats',array('pid'=>$data['id'], 'month_of_download'=>date('Y-M'),'year'=>date('Y'),'month'=>date('m'),'day'=>date('d'), 'timestamp'=>time(), 'ip'=>$ip),array('%s','%s','%s','%s','%s'));]
谢谢,

埃里克

这是处理函数代码的一部分

else {    

    $id = (int)$id;
    $data = $wpdb->get_row("select * from ahm_files where id='$id'",ARRAY_A);
}




    if($data['download_count']>=$data['quota']&&$data['quota']>0)        wp_die('Download Limit Excedded!');

    //added for download monitor import feature
    $data['file'] = str_replace(site_url('/'),ABSPATH, $data['file']);

    if(strpos($data['file'],'ttp://')){
        header("location: ".$data['file']);
        die();
    }
    $data['file'] = trim($data['file']);
    if(file_exists($data['file']) && $data['file']!= "")
    $fname = $data['file'];    
    else if(file_exists(UPLOAD_DIR . $data['file']) && $data['file']!= "")
    $fname = UPLOAD_DIR . $data['file'];
    else if( $data['file']== "")
        wp_die("No file attached yet.");
    else 
    wp_die('File not found!');



    $wpdb->query("update ahm_files set download_count=download_count+1 where id='$data[id]'");




    $wpdb->insert('wp_ahm_download_stats',array('pid'=>$data['id'], 'month_of_download'=>date('Y-M'),'year'=>date('Y'),'month'=>date('m'),'day'=>date('d'), 'timestamp'=>time(), 'ip'=>$ip),array('%s','%s','%s','%s','%s'));



    $filetype = wp_check_filetype($fname);

    $mtype = $filetype['type'];

    $asfname = basename($fname);

    $fsize = filesize($fname);

虽然有可能,但你试图做的是让你最初的问题复杂化

如果您想要的一切都是跟踪用户的行为,在本例中,就是从哪个页面下载,那么任务就简单多了:使用谷歌分析。

基本上,您向用户执行的操作添加一个onclick参数,触发器将向Google Analytics发送一个特殊事件,您可以在其中访问源(您的页面)、位置等

代码如下所示:

<a href="#" onClick="_gaq.push(['_trackEvent', 'Download Event', 'Download', 'Title of the Downloaded File']);">Download</a>
这家伙的功劳

或:

另一种方法是:

<?php 

$slug = $post->post_name;

$my_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '$slug'"); ?>
提供了
get_The_ID()
函数来获取当前帖子的ID:

<?php $postid = get_the_ID(); ?> 


您好,谢谢您的回复,我不想只跟踪,因为我已经处理了这个问题,它正在将文档ID、日期和时间戳以及其他细节保存到我的跟踪表中,但我没有正确选择插入短代码的帖子ID。一旦我能够提取下载短代码所在的post_id,它也将使我能够保存post id,因为我需要跟踪表中的post id与wp_post表中的post id连接。这就是为什么我如此需要它。Regard我更新了我的答案,引入了一种通过slug获取页面ID的方法,你只需要通过$post->post\u name获取slug;嗨,谢谢。我已经这样做了,但它仍然返回零ID。可能是我把它放在了错误的循环中吗?我会试着把它放在数据处理循环之上,看看它会返回什么。。。谢谢
<?php $postid = get_the_ID(); ?>