2012-04-12 9 views
13

Ich entwickle eine WordPress-Website, die einen json-Feed für eine Mobile App haben wird. Ich legte das Back-End mit erweiterten benutzerdefinierten Feldern mit einem Repeater-Feld für mehrere Artikel und mehrere Seiten innerhalb jedes Artikels. http://www.advancedcustomfields.com/add-ons/repeater-field/ Screen shot of Repeater FieldsJSON API, um erweiterte benutzerdefinierte Felder anzuzeigen - WordPress

Ich bin mit dem JSON-API, aber dies schließt nicht jede meiner benutzerdefinierten Felder. Gibt es derzeit ein Plugin, das das kann?

These are the Custom Fields 'slug names' of the previous fields

+1

Ich habe das gleiche magazing-Projekt für ipad und mein WebAdmin ist wordpress, können Sie bitte erzählen mir, wie Sie diese ACF-Ausgänge zu JSON verwaltet haben? ... – Denish

Antwort

8

hier kam mit der gleichen Frage zu suchen. Dies ist noch nicht vollständig überprüft, aber ich denke, dass dies auf dem richtigen Weg ist. Hör zu.

Ich habe eine weniger verschachtelte Ebene als Sie tun, damit dies etwas geändert werden muss. Aber das JSON-API-Plugin hat einen Filter namens json_api_encode. Ich habe einen Repeater namens Spezifikationen, der so aussieht.

http://d.pr/i/YMvv

In meinen Funktionen Datei ich das habe.

add_filter('json_api_encode', 'my_encode_specs'); 

function my_encode_specs($response) { 
    if (isset($response['posts'])) { 
    foreach ($response['posts'] as $post) { 
     my_add_specs($post); // Add specs to each post 
    } 
    } else if (isset($response['post'])) { 
    my_add_specs($response['post']); // Add a specs property 
    } 
    return $response; 
} 

function my_add_specs(&$post) { 
    $post->specs = get_field('specifications', $post->id); 
} 

Dies hängt einen benutzerdefinierten Wert an die JSON-API-Ausgabe an. Beachten Sie, dass die get_field-Funktion von ACF hier perfekt funktioniert, um das Array der Repeater-Werte zurück zu holen.

Hoffe, das hilft!

+0

Großartiges Beispiel! Danke vielmals. Hattest du eine Änderung, um mehr Arbeit mit JSON API zu tun? Noch mehr Tipps für benutzerdefinierte Felder/Post-Meta? – Iladarsda

16

@Myke: Sie haben mir enorm geholfen. Hier ist mein bescheidener Zusatz:

add_filter('json_api_encode', 'json_api_encode_acf'); 


function json_api_encode_acf($response) 
{ 
    if (isset($response['posts'])) { 
     foreach ($response['posts'] as $post) { 
      json_api_add_acf($post); // Add specs to each post 
     } 
    } 
    else if (isset($response['post'])) { 
     json_api_add_acf($response['post']); // Add a specs property 
    } 

    return $response; 
} 

function json_api_add_acf(&$post) 
{ 
    $post->acf = get_fields($post->id); 
} 
+1

Das ist brilliant – benpalmer

+0

Awesome - arbeitete wie ein Charme !! – whodeee

+0

LEBENSRETTER! Es dauerte ein bisschen, bis ich herausgefunden hatte, wo ich es hinstellen musste. Ich musste fast alle JSON-APIs für die WP-Dokumentation durchlesen. (json-api.php) –

3

Ich bin nicht sicher, wenn Sie in einer Lösung immer noch interessiert sind, aber ich konnte die json-api-Plugin Modelle/post.php Datei ändern, um die Repeater-Daten anzuzeigen, wie eine Anordnung. Dies ist eine Modifikation einer Modifikation hergestellt von http://wordpress-problem.com/marioario-on-plugin-json-api-fixed-get-all-custom-fields-the-right-way/

ersetzen die set_custom_fields_value() -Funktion mit dem folgenden:

function set_custom_fields_value() { 

    global $json_api; 

    if ($json_api->include_value('custom_fields') && $json_api->query->custom_fields) { 

     // Query string params for this query var 
     $params = trim($json_api->query->custom_fields); 

     // Get all custom fields if true|all|* is passed 
     if ($params === "*" || $params === "true" || $params === "all") { 

      $wp_custom_fields = get_post_custom($this->id); 
      $this->custom_fields = new stdClass(); 

      // Loop through our custom fields and place on property 
      foreach($wp_custom_fields as $key => $val) { 
       if (get_field($key)) { 
        $this->custom_fields->$key = get_field($key); 
       } else if ($val) { 
        // Some fields are stored as serialized arrays. 
        // This method does not support multidimensionals... 
        // but didn't see anything wrong with this approach 
        $current_custom_field = @unserialize($wp_custom_fields[$key][0]); 

        if (is_array($current_custom_field)) { 

         // Loop through the unserialized array 
         foreach($current_custom_field as $sub_key => $sub_val) { 

          // Lets append these for correct JSON output 
          $this->custom_fields->$key->$sub_key = $sub_val; 
         } 

        } else { 

         // Break this value of this custom field out of its array 
         // and place it on the stack like usual 
         $this->custom_fields->$key = $wp_custom_fields[$key][0]; 

        } 
       } 
      } 
     } else { 

      // Well this is the old way but with the unserialized array fix 
      $params = explode(',', $params); 
      $wp_custom_fields = get_post_custom($this->id); 
      $this->custom_fields = new stdClass(); 

      foreach ($params as $key) { 

       if (isset($wp_custom_fields[$key]) && $wp_custom_fields[$key][0]) { 
        $current_custom_field = @unserialize($wp_custom_fields[$key][0]); 

        if (is_array($current_custom_field)) { 
         foreach($current_custom_field as $sub_key => $sub_val) { 
          $this->custom_fields->$key->$sub_key = $sub_val; 
         } 

        } else { 
         $this->custom_fields->$key = $wp_custom_fields[$key][0]; 

        } 

       } 
      } 
     } 

    } else { 
     unset($this->custom_fields); 

    } 
} 
0

Die aktuelle Version des ACF druckt ein custom_fields auf die JSON-API auf einen Anruf-Objekt, das alle die Felder relativ zum Post oder zur Seite. Ich habe die @Myke-Version bearbeitet, um bestimmte benutzerdefinierte Felder von der ACF-Optionsseite zu jedem Post oder jeder Seite hinzuzufügen. Leider gibt es keine get_fields() - Funktion für die gesamte Optionsseite, so dass Sie sie abhängig von Ihrer Feldstruktur bearbeiten müssen.

add_filter('json_api_encode', 'json_api_encode_acf'); 

function json_api_encode_acf($response) { 
    if (isset($response['posts'])) { 
     foreach ($response['posts'] as $post) { 
      json_api_add_acf($post); // Add specs to each post 
     } 
    } 
    else if (isset($response['post'])) { 
     json_api_add_acf($response['post']); // Add a specs property 
    } 
    else if (isset($response['page'])) { 
     json_api_add_acf($response['page']); // Add a specs to a page 
    } 

    return $response; 
} 

function json_api_add_acf(&$post) { 
    $post->custom_fields->NAME_OF_YOUR_CUSTOM_FIELD = get_field('NAME_OF_YOUR_CUSTOM_FIELD', 'option'); 
} 
4

Update für Wordpress 4.7

Mit der Veröffentlichung von Wordpress 4.7 die REST-Funktionalität wird nicht mehr als eigenständiges Plugin zur Verfügung gestellt, sondern sein gerollt (kein Plugin erforderlich).

Die vorherigen Filter scheinen nicht zu funktionieren. Das folgende Snippet jedoch (kann in Ihrem functions.php sein):

> = PHP 5.3

add_filter('rest_prepare_post', function($response) { 
    $response->data['acf'] = get_fields($response->data['id']); 
    return $response; 
}); 

< PHP 5,3

add_filter('rest_prepare_post', 'append_acf'); 

function append_acf($response) { 
    $response->data['acf'] = get_fields($response->data['id']); 
    return $response; 
}; 

Hinweis der Filter ein Wildcard-Filter, wie

apply_filters("rest_prepare_$type", ... 

so angewendet, wenn Sie mehrere Inhaltstypen (custom) haben, Sie müssen Folgendes tun:

add_filter('rest_prepare_multiple_choice', 'append_acf'); 
add_filter('rest_prepare_vocabularies', 'append_acf'); 

function append_acf($response) { 
    $response->data['acf'] = get_fields($response->data['id']); 
    return $response; 
}; 

Hinweis Es scheint, dass rest_prepare_xpro Datensatz genannt wird. Wenn Sie also den Index-Endpunkt anpingen, wird er mehrmals aufgerufen (Sie müssen also nicht überprüfen, ob seine Posts oder Posts vorhanden sind)

Verwandte Themen