Skip to content

Instantly share code, notes, and snippets.

@sivasankars
Last active June 12, 2018 18:08
Show Gist options
  • Save sivasankars/ea70657e5d39dfc94518efcbd2851e87 to your computer and use it in GitHub Desktop.
Save sivasankars/ea70657e5d39dfc94518efcbd2851e87 to your computer and use it in GitHub Desktop.
List Posts From WordPress Using REST API With cURL In PHP (http://niralar.com/list-posts-from-wordpress-using-rest-api-with-curl-in-php/)
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="List Posts From WordPress Using REST API With cURL In PHP">
<title>Demo - WordPress REST API</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body class="bg-light">
<div class="container">
<div class="py-5 text-center">
<img class="d-block mx-auto mb-4" src="http://niralar.com/wp-content/uploads/logo.png" alt="niralar">
<p class="lead">List Posts From WordPress Using REST API With cURL In PHP</p>
</div>
<div class="row">
<div class="col-md-12 order-md-1">
<div class="list-group">
<?php
$url="http://niralar.com/wp-json/wp/v2/posts";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
$posts = json_decode($result, true);
foreach ($posts as $post) { ?>
<a href="<?php echo $post['link']; ?>" class="list-group-item list-group-item-action flex-column align-items-start">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1"><?php echo $post['title']['rendered']; ?></h5>
<small><?php echo date('F j, Y', strtotime($post['date'])); ?></small>
</div>
<p class="mb-1"><?php echo $post['excerpt']['rendered']; ?></p>
</a>
<?php } ?>
</div>
</div>
</div>
<footer class="my-5 pt-5 text-muted text-center text-small">
<p class="mb-1">Copyright &copy; 2018 Niralar</p>
</footer>
</div>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>
@kostyukdv
Copy link

Hi,
Thanks for sharing a cool tutorial.
Maybe you can add featured image too?
Regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment