V
V
Valentin Popov2021-09-28 00:18:56
PHP
Valentin Popov, 2021-09-28 00:18:56

How to display json data on wordpress home page?

Hello. I have json with this content

"{'count':'13','updated_at':'2021-09-27 10:07:23'}"


I want to display data from json on my front page (main static page)
I'm trying to do it like this:
<?php 
$data = file_get_contents( 'https://site.com/json_config/count.json' );
   
 if ( ! empty( $data ) ) {
            $args = json_encode( $data );
            

           foreach ($args as $el) {
               print_r($el->count);
           }


        }
?>


But for some reason nothing works. Tell me what I'm doing wrong

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nadim Zakirov, 2021-09-28
@elarkov

Try like this:

<?php

header('Content-Type: text/plain; charset=UTF-8');

$data = file_get_contents('https://site.com/json_config/count.json');
   
if ($data !== false) {
    $arr = json_decode($data, true);
    echo "Из конфига получены следующие данные:\n";
    echo print_r($arr, true);
}

else {
    echo "Не удалось прочитать файл конфигурации!";
}

?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question