在get_attached_file wordpress中使用get_the_id时出错

时间:2016-10-27 04:33:05

标签: wordpress

每当我尝试在get_attached_file()中使用get_the_ID()时,我总是得到空白的响应示例get_attached_file( get_the_ID() )

但如果我使用像get_attached_file( 4125 )这样的正常数字,那就完全可以工作

我应该怎么做才能解决它?

我现在的代码:

switch ($command) {
    case 'list_product':

        $loop = new WP_Query( 
                array(
                        'post_type'    => 'product'
                    )
                ); 
        if( $loop->have_posts() ) :

            $data = array( "api_status" => 1, "api_message" => "success");
            // First we get the image id
            // $post_thumbnail_id = get_post_thumbnail_id( get_the_ID() ); 

            // // Then we get the image data, we will get an array with some informations
            // $image = wp_get_attachment_image_src( $post_thumbnail_id, 'large' );

            // // the image url is the first index of this array
            // $image_url = $image[0];

            $meta = array();
            while ( $loop->have_posts() ) : $loop->the_post();

                    $meta[] = array(
                        "id"            => get_the_ID(),
                        "post_name"     => get_the_title(),
                        "stock_status"  => get_post_meta( get_the_ID(), '_stock_status', true ),
                        "price"         => get_post_meta( get_the_ID(), '_price', true ),
                        "reguler_price" => get_post_meta( get_the_ID(), '_regular_price', true ),
                        // "image"          => basename( get_attached_file( $post_id ) ),
                    );
            endwhile;
        endif;
        echo  json_encode($meta);
        break;

3 个答案:

答案 0 :(得分:2)

根据this article,看看是否有效:

<?php
public function getDistanceReport($companyId,$vehicleId,$fromDate,$toDate,$offset){
  $fromDate, $toDate,$speed,null);
    $timewhere="(DATE_ADD( mt.timestamp, INTERVAL mt.gmtTimediff MINUTE ) >='$fromDate') order by mt.timestamp asc limit 1";
    $where = "v.companyId='".$companyId."' and v.status=1 and mt.vehicleId='".$vehicleId."' and ".$timewhere;

    try{


         $sql_s="select mt.location as startLocation,DATE_ADD(mt.timestamp, INTERVAL mt.gmtTimediff MINUTE ) as startTime,v.vehicleNo,mt.distance as startDistance,v.vehicleTypeNew from fm_mt90  mt
                inner join fm_vehicle v on mt.vehicleId=v.vehicleId
                left join fm_driver d on d.driverId = v.driverId
                where ".$where;

        $startdata=$this->db->fetchRow($sql_s);


        $timewhere1="(DATE_ADD( mt.timestamp, INTERVAL mt.gmtTimediff MINUTE ) <='$toDate') order by mt.timestamp DESC limit 1";
        $where = "v.companyId='".$companyId."' and v.status=1 and mt.vehicleId='".$vehicleId."' and ".$timewhere1;

         $sql_s1="select mt.location as stopLocation,DATE_ADD(mt.timestamp, INTERVAL mt.gmtTimediff MINUTE ) as stopTime,mt.distance as stopDistance from fm_mt90  mt
                inner join fm_vehicle v on mt.vehicleId=v.vehicleId
                left join fm_driver d on d.driverId = v.driverId
                where ".$where;

        $stopdata=$this->db->fetchRow($sql_s1);

        if($stopdata['stopDistance']==null){
            $stopdata['stopDistance']=0;
        }
        if($startdata['startDistance']==null){
            $stopdata['startDistance']=0;
        }
        if($stopdata['stopDistance']>0){
            $stopdata['distanceTravelled']=$stopdata['stopDistance']-$startdata['startDistance'];
        }else{
            $stopdata['distanceTravelled']=0;
        }
        $vehicledata=array_merge($startdata,$stopdata);

        return $vehicledata;





    }catch(Exception $e){
        echo $e;
       echo $e->getMessage();
      return false;

    }    
}

答案 1 :(得分:1)

您可以尝试先将ID分配给变量 -

$meta = array();
            while ( $loop->have_posts() ) : $loop->the_post();
                    $ID = get_the_ID()
                    $meta[] = array(
                        "id"            => $ID,
                        "post_name"     => get_the_title(),
                        "stock_status"  => get_post_meta( $ID, '_stock_status', true ),
                        "price"         => get_post_meta( $ID, '_price', true ),
                        "reguler_price" => get_post_meta( $ID, '_regular_price', true ),
                        "image"         => basename( get_attached_file( $ID ) ),
                    );
            endwhile;

答案 2 :(得分:0)

声明$post变量全局并使用$post->ID代替$IDget_the_id,希望此帮助