This pages demonstrates how to get JSON with a “quote of the day”, show the quote and store it in a Custom Post Page.
- Data-Source:
API: https://quotes.rest/ and https://quotes.rest/qod
Local stored JSON: https://api.json-content-importer.com/extra/json/Quote_of_the_day.json - Create a new JCI-Template:
- Name of template: quote-of-the day
- URL: https://quotes.rest/qod
- Curloptions (otherwise you get XML):
CURLOPT_HTTPHEADER=accept:application/json##content-type: application/json
twig-template:
{% set nowtime = "now"| date("U") %} {% set dateofquote = contents.quotes.0.date | date("d.m.Y") %} {% set dateofquotetime = contents.quotes.0.date | date("U") %} {% set author = contents.quotes.0.author %} {% set quote= (contents.quotes.0.quote | slice(0,10)) ~ '... (we use only the first 10 chars of the quote, see more at <a href=" https://theysaidso.com/" target="_blank">https://theysaidso.com/</a>)' %} {% set id = contents.quotes.0.id %} {% set cont = '<center><strong>Quote of '~dateofquote~' by '~author~':</strong><br>'~quote~'</center>' %} {% set cont = cont ~ '<hr><a href="/pro-quote-of-the-day/">see how this page was generated out of JSON</a>' %} {{cont}} <hr> {% set slug = 'cptquoteoftheday' %} {% set taxonomyslug = 'quoteofthedayauthors' %} is there any stored-quote of the day {{dateofquote}}?<br> {% set cpids = wp_get_cp_by_cpf_keyvalue(slug , "date", dateofquotetime , FALSE) %} <!--{{cpids | dump }}--> {% if cpids %} yes there is already one: nothing to do {% set rescpf = wp_insert_custom_field_keyvalue(cpids.0, 'latestupdate', nowtime, FALSE) %} {% else %} create a new cpt:<br> {% set titel = dateofquote ~': '~author ~ '- ' ~ nowtime %} {% set pageslug= 'name-'~author %} {% set postStatusUsed= 'publish' %} {% set newpageid = wp_new_custom_post(slug, titel, pageslug, cont, "", dateofquote, postStatusUsed, 1, FALSE) %} newpageid : {{newpageid }}<br> {% set rescpf = wp_insert_custom_field_keyvalue(newpageid , 'date', dateofquotetime, FALSE) %} {% set rescpf = wp_insert_custom_field_keyvalue(newpageid , 'author', author, FALSE) %} {% set rescpf = wp_insert_custom_field_keyvalue(newpageid , 'quote', quote, FALSE) %} {% set tax = wp_insert_taxonomy(newpageid, taxonomyslug, author, FALSE) %} {% endif %}
3. Set up a Custom Post Type with custom Post Fields
You might use a Plugin like “Custom Post Type UI” for a new Custom Post Type with slug “cptexample”.
And ACF for Custom Post Fields to set CPFs with the slugs “quote”, “date” and “author”.
The slugs for the CPT and the CPFs are used in the twig-template!
4. In Action:
When you w… (we use only the first 10 chars of the quote, see more at https://theysaidso.com/)
see how this page was generated out of JSON
is there any stored-quote of the day 30.01.2022?
yes there is already one: nothing to do