2016-10-27 2 views
1

, wenn ich versuche, get_the_ID() innerhalb get_attached_file() zu verwenden i immer leer Antwort Beispiel get_attached_file(get_the_ID())Fehler bei der Verwendung get_the_id in innen get_attached_file Wordpress

bekommen, aber wenn ich wie get_attached_file(4125) normale Zahl verwenden ihre Arbeit totaly

was sollte ich tun, um es zu reparieren?

meinen Code jetzt:

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; 

Antwort

1

Sie können versuchen, die ID in eine Variable zuerst zuweisen -

$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; 
+0

ich es versucht habe, bevor ich mein Problem zu veröffentlichen, und in der Tat die Spitzen u mir seine nicht funktioniert geben, dazu noch andere Lösung? –

2

Nach this article sehen, ob das funktioniert:

$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; 
+0

Ich benutze 'Produkt' (regulär) als post_type. Kann ich zwei post_type für 1 Abfrage verwenden? –

+0

hmm, sorry sir, ich bekomme immer noch eine leere Antwort in 'image' –

0

eine $post Variable global deklarieren und verwenden $post->ID statt $ID oder get_the_id, hoffen, dass diese Hilfe

Verwandte Themen