Skip to content

Commit f419597

Browse files
committed
New feature - Fetch posts from publications
1 parent fa79a0f commit f419597

File tree

1 file changed

+69
-32
lines changed

1 file changed

+69
-32
lines changed

display-medium-posts.php

Lines changed: 69 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function run_display_medium_posts() {
7676

7777
// Example 1 : WP Shortcode to display form on any page or post.
7878
function posts_display($atts){
79-
$a = shortcode_atts(array('handle'=>'-1', 'default_image'=>'http://i.imgur.com/p4juyuT.png', 'display' => 3, 'offset' => 0, 'total' => 10, 'list' => false), $atts);
79+
$a = shortcode_atts(array('handle'=>'-1', 'default_image'=>'http://i.imgur.com/p4juyuT.png', 'display' => 3, 'offset' => 0, 'total' => 10, 'list' => false, 'publication' => false), $atts);
8080
// No ID value
8181
if(strcmp($a['handle'], '-1') == 0){
8282
return "";
@@ -87,44 +87,81 @@ function posts_display($atts){
8787
$offset = $a['offset'];
8888
$total = $a['total'];
8989
$list = $a['list'] =='false' ? false: $a['list'];
90+
$publication = $a['publication'] =='false' ? false: $a['publication'];
9091

91-
$data = file_get_contents("https://medium.com/".$handle."/latest?format=json");
92+
$data = file_get_contents("https://medium.com/".$handle."/latest?format=json");
9293
$data = str_replace("])}while(1);</x>", "", $data);
94+
if($publication) {
95+
//If handle provided is specified as a publication
96+
$json = json_decode($data, true);
97+
98+
$json = json_decode($data);
99+
$posts = $json->payload->posts;
100+
$items = array();
101+
$count = 0;
102+
foreach($posts as $post)
103+
{
104+
$items[$count]['title'] = $post->title;
105+
$items[$count]['url'] = 'https://medium.com/'.$handle.'/'.$post->uniqueSlug;
106+
$items[$count]['subtitle'] = isset($post->virtuals->subtitle) ? $post->virtuals->subtitle : "";
107+
if(!empty($post->virtuals->previewImage->imageId))
108+
{
109+
$image = 'http://cdn-images-1.medium.com/max/500/'.$post->virtuals->previewImage->imageId;
110+
}
111+
else {
112+
$image = $default_image;
113+
}
114+
$items[$count]['image'] = $image;
115+
$items[$count]['duration'] = round($post->virtuals->readingTime);
116+
$items[$count]['date'] = isset($post->firstPublishedAt) ? date('Y.m.d', $post->firstPublishedAt/1000): "";
93117

94-
$json = json_decode($data, true);
95-
96-
$json = json_decode($data);
97-
$posts = $json->payload->references->Post;
98-
$items = array();
99-
$count = 0;
100-
foreach($posts as $post)
101-
{
102-
$items[$count]['title'] = $post->title;
103-
$items[$count]['url'] = 'https://medium.com/'.$handle.'/'.$post->uniqueSlug;
104-
$items[$count]['subtitle'] = isset($post->content->subtitle) ? $post->content->subtitle : "";
105-
if(!empty($post->virtuals->previewImage->imageId))
118+
$count++;
119+
}
120+
if($offset)
106121
{
107-
$image = 'http://cdn-images-1.medium.com/max/500/'.$post->virtuals->previewImage->imageId;
122+
$items = array_slice($items, $offset);
108123
}
109-
else {
110-
$image = $default_image;
124+
125+
if(count($items) > $total)
126+
{
127+
$items = array_slice($items, 0, $total);
128+
}
129+
}
130+
else {
131+
$json = json_decode($data, true);
132+
133+
$json = json_decode($data);
134+
$posts = $json->payload->references->Post;
135+
$items = array();
136+
$count = 0;
137+
foreach($posts as $post)
138+
{
139+
$items[$count]['title'] = $post->title;
140+
$items[$count]['url'] = 'https://medium.com/'.$handle.'/'.$post->uniqueSlug;
141+
$items[$count]['subtitle'] = isset($post->content->subtitle) ? $post->content->subtitle : "";
142+
if(!empty($post->virtuals->previewImage->imageId))
143+
{
144+
$image = 'http://cdn-images-1.medium.com/max/500/'.$post->virtuals->previewImage->imageId;
145+
}
146+
else {
147+
$image = $default_image;
148+
}
149+
$items[$count]['image'] = $image;
150+
$items[$count]['duration'] = round($post->virtuals->readingTime);
151+
$items[$count]['date'] = isset($post->createdAt) ? date('Y.m.d', $post->createdAt/1000): "";
152+
153+
$count++;
154+
}
155+
if($offset)
156+
{
157+
$items = array_slice($items, $offset);
111158
}
112-
$items[$count]['image'] = $image;
113-
$items[$count]['duration'] = round($post->virtuals->readingTime);
114-
$items[$count]['date'] = isset($post->createdAt) ? date('Y.m.d', $post->createdAt/1000): "";
115-
116-
$count++;
117-
}
118-
if($offset)
119-
{
120-
$items = array_slice($items, $offset);
121-
}
122-
123-
if(count($items) > $total)
124-
{
125-
$items = array_slice($items, 0, $total);
126-
}
127159

160+
if(count($items) > $total)
161+
{
162+
$items = array_slice($items, 0, $total);
163+
}
164+
}
128165
?>
129166
<div id="display-medium-owl-demo" class="display-medium-owl-carousel">
130167
<?php foreach($items as $item) { ?>

0 commit comments

Comments
 (0)