down00112 2014-11-27 20:02
浏览 36

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

How can I retrieve the Post or Page ID and store it in a variable to be used anywhere like within a shortcode processing data?

I have a function that processes a download button and would like to store the Page/Post ID that the download was processed together with timestamp in order that I do some tracking on which page/post the download was made.

Here is the data that is currently being stored and I would like to add the post_ID() data as well...

$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'));]

Thank you,

Eric

This is part of the code of the processing function..

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);
  • 写回答

2条回答 默认 最新

  • duanmanmian7589 2014-11-27 22:30
    关注

    Although possible, what you're trying to do is kind of complicating your initial problem.

    If everything you want is to track the behavior of the user, in this case, from which page the download was done, the task is much easier: Use Google Analytics Event Tracking.

    Basically you add an onclick parameter to the action the user does, and the trigger will send a special event to Google Analytics where you can access the source (your page), the location, etc.

    The code would look something like this:

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

    EDIT - Since the my answer above doesn't solve the problem, here's how you can get the ID of the post:

    So, the main function to get the ID, as the answer below states is get_the_id(), however if you can't get it, there are other ways:

    By slug:

    // Usage:
    // get_id_by_slug('any-page-slug');
    
    function get_id_by_slug($page_slug) {
        $page = get_page_by_path($page_slug);
        if ($page) {
            return $page->ID;
        } else {
            return null;
        }
    }
    

    Credits to the guy here

    OR:

    Another way to do it:

    <?php 
    
    $slug = $post->post_name;
    
    $my_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '$slug'"); ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染