Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用get_时出错在get_attached_文件wordpress中使用get_id_Wordpress - Fatal编程技术网

使用get_时出错在get_attached_文件wordpress中使用get_id

使用get_时出错在get_attached_文件wordpress中使用get_id,wordpress,Wordpress,每当我尝试在get_attached_文件()中使用get_the_ID(),我总是得到空白响应示例get_attached_文件(get_the_ID()) 但是如果我使用普通的数字,比如get\u attached\u file(4125)它的全部工作 我该怎么做才能修好它 我现在的代码: 开关($command){ “产品清单”案例: $loop=新的WP\U查询( 排列( “post_类型”=>“产品” ) ); 如果($loop->have_posts()): $data=array

每当我尝试在get_attached_文件()中使用get_the_ID(),我总是得到空白响应示例
get_attached_文件(get_the_ID())

但是如果我使用普通的数字,比如
get\u attached\u file(4125)
它的全部工作

我该怎么做才能修好它

我现在的代码:

开关($command){
“产品清单”案例:
$loop=新的WP\U查询(
排列(
“post_类型”=>“产品”
)
); 
如果($loop->have_posts()):
$data=array(“api_状态”=>1,“api_消息”=>success”);
//首先我们得到图像id
//$post_thumbnail_id=get_post_thumbnail_id(get_the_id());
////然后我们得到图像数据,我们将得到一个包含一些信息的数组
//$image=wp_get_attachment_image_src($post_缩略图_id,'large');
////图像url是此数组的第一个索引
//$image_url=$image[0];
$meta=array();
而($loop->have_posts()):$loop->the_post();
$meta[]=数组(
“id”=>获取该id(),
“post_name”=>获取标题(),
“库存状态”=>get_post_meta(get_ID(),“库存状态”,true),
“price”=>get_post_meta(get_ID(),“'u price',true),
“reguler\u price”=>获取发布元数据(获取ID(),“常规价格”,true),
//“image”=>basename(获取附加的文件($post\u id)),
);
结束时;
endif;
echo json_编码($meta);
打破

您可以先尝试将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;
据了解,这是否有效:

$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();

                    //get the first attachment. Not sure if this is the one you want
                    $args = array(
                        'post_type' => 'attachment',
                        'numberposts' => -1,
                        'post_status' => null,
                        'post_parent' => $post->ID
                     );

                    $attachments = get_posts( $args );   
                    $attachment_ID = $attachments[0]->ID;    

                    $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( $attachment_ID ) ),
                    );
            endwhile;

声明一个
$post
变量为全局变量,并使用
$post->ID
而不是
$ID
获取ID
,希望这有帮助

我在发布我的问题之前已经尝试过,事实上你给我的提示不起作用,有其他解决方法吗?我使用
产品
(常规)作为post\u类型。我可以使用两个post\u类型进行一次查询吗?嗯,对不起,先生,我在
图像中仍然得到空白响应