2012-04-12 17 views
13

Sto sviluppando un sito WordPress per riviste che avrà un feed JSON per un'app mobile. Ho impostato il backend usando Advanced Custom Fields con un campo Repeater per più articoli e più pagine all'interno di ciascun articolo. http://www.advancedcustomfields.com/add-ons/repeater-field/ Screen shot of Repeater FieldsAPI JSON per mostrare campi personalizzati avanzati - WordPress

Sto usando l'API JSON ma questo non include nessuno dei miei campi personalizzati. C'è attualmente un plugin che può fare questo?

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

+1

ho lo stesso progetto di archiviazione per ipad e il mio WebAdmin è wordpress, puoi dirlo come hai gestito questo output ACF su JSON? ... – Denish

risposta

8

Siamo venuti qui con la ricerca con la stessa domanda. Questo non è ancora completamente controllato, ma penso che stia andando sulla strada giusta. Controlla.

Ho un livello nidificato inferiore rispetto a quello che fai, quindi potrebbe essere necessario un po 'di modifiche. Ma il plugin API JSON ha un filtro chiamato json_api_encode. Ho un ripetitore chiamato specifiche che assomiglia a questo.

http://d.pr/i/YMvv

Nelle mie funzioni di file ho questo.

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); 
} 

Quale aggiunge un valore personalizzato all'output dell'API JSON. Notare che la funzione get_field di ACF funziona perfettamente qui per riportare la matrice dei valori del ripetitore.

Spero che questo aiuti!

+0

Ottimo esempio! Grazie molto. Hai avuto una modifica per lavorare ancora di più con l'API JSON? Altri suggerimenti per campi personalizzati/post meta? – Iladarsda

16

@ Myke: mi hai aiutato moltissimo. Ecco la mia modesta aggiunta:

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

Questo è brillante – benpalmer

+0

Fantastico - ha funzionato come un fascino !! – whodeee

+0

LIFESAVER! Mi ci è voluto un po 'per scoprire dove metterlo, ho dovuto leggere quasi tutte le API di JSON per la documentazione di WP. (json-api.php) –

3

Non sono sicuro se sei ancora interessato a una soluzione, ma sono stato in grado di modificare il file di modelli JSON-plugin API/post.php per visualizzare i dati di repeater come un array. Questa è una modifica di una modifica apportata dal http://wordpress-problem.com/marioario-on-plugin-json-api-fixed-get-all-custom-fields-the-right-way/

sostituire la funzione set_custom_fields_value() con il seguente:

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

versione attuale della ACF stampa un custom_fields oggetto su una chiamata alla API JSON, contenente tutti i campi relativi al Post o alla Pagina. Ho modificato la versione di @Myke per aggiungere campi personalizzati specifici dalla pagina Opzione ACF a ciascun post o pagina. Sfortunatamente non esiste la funzione get_fields() per l'intera pagina delle opzioni, quindi dovrai modificarla in base alla struttura dei tuoi campi.

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

Aggiornamento per Wordpress 4.7

Con il rilascio di Wordpress 4.7 la funzionalità REST non è più previsto come un plugin distinta, piuttosto il suo laminati a (nessun plug-in necessario).

I filtri precedenti non sembrano funzionare. Tuttavia, il seguente frammento di codice (può essere nel tuo functions.php):

> = 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; 
}; 

Nota il filtro è un filtro wild card, applicato come

apply_filters("rest_prepare_$type", ... 

quindi se si dispone di più tipi di contenuto (personalizzati), Avrai bisogno di fare:

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; 
}; 

Nota Sembra che rest_prepare_x si chiama per record. Quindi, se esegui il ping dell'endpoint dell'indice, verrà chiamato più volte (quindi non devi controllare se i suoi post o post)

Problemi correlati