Mammothdesign - logo

Zaciąganie danych JSON w PHP

Wyświetlanie wszystkich danych

PHP
<?php 
$request = wp_remote_get( 'https://mammothdesign.pl/wp-json/wp/v2/blog' );
$body = wp_remote_retrieve_body( $request );
$arr = json_decode( $body,true );

$string = '';
foreach( $arr as $value ) {

  $json_id = $value['id'];
  $string .= 'id wpisu: '.$json_id;

  $json_title = $value['title'];
  $string .= 'tyuł wpisu: ';
    foreach( $json_title as $tytul ){ 
       $string .= $tytul.',';
    }

  $json_categories = $value['categories'];
  $string .= 'kategorie: ';
    foreach( $json_categories as $kategoria ){
      $string .= $kategoria.',';
      if ($kategoria == '120'){
        $string .= '<br>jest css we wpisie'.$value['title']['rendered'];
      }
}

echo $string.'<br><br>';
unset($string);
}
?>

Wyświetlanie danych z paginacją

PHP
<?php 
$request = wp_remote_get( 'https://mammothdesign.pl/wp-json/wp/v2/blog' );
$body = wp_remote_retrieve_body( $request );
$arr = json_decode( $body,true );

$nb_elem_per_page = 3;
$page = isset($_GET['page'])?intval($_GET['page']-1):0;
$number_of_pages = intval(count($arr)/$nb_elem_per_page)+1;


foreach (array_slice($arr, $page*$nb_elem_per_page, $nb_elem_per_page) as $value) { 
  echo $value['title']['rendered'].'<br>';
}

for($i=1;$i<$number_of_pages;$i++){
  if ($i === ($page + 1)){$active = 'active';}
  echo '<li class="'.$active.'"><a href="./?page='.$i.'">'.$i.'</a></li>';
  unset($active);
}
?>
Strona główna bloga