Retrieve and show data from a blog post URL using custom module

Hi,

Please check the attached image. I’ve created a custom module using the below code to show 2 recent posts from a blog inside a website page. But our new requirement is to show same data from some posts which we input manually. So is it possible to fetch Feature image, Title and Content from a specific blog post URL? It is ok if we need to repeat the custom module twice inside the template.

{% set rec_posts = blog_recent_posts(‘default’, 2) %}
<div class=“row-fluid-wrapper”>
<div class=“row-fluid”>

{% for rec_post in rec_posts %}
<div class=“span12”>
<a href=“{{rec_post.absolute_url}}” class=“blog-post”>
<div class=“span6”>
<div class=“blog-img-outer”>
<img src=“{{ rec_post.post_list_summary_featured_image }}” alt=“{{ rec_post.featured_image_alt_text }}”>
</div>
</div>
<div class=“span6”>
<h3>{{ rec_post.name }}</h3>
<h3>{{ rec_post.show_date }}</h3>
<p>
{{ rec_post.post_summary|striptags|truncate(150,false,‘…’) }}
</p>
<span class=“read-more”>Read More</span>
</div>
</a>
</div>

{% endfor %}
</div>
</div>

Thanks

Shiyon

Hey @shiyon ,
I don’t believe you can fetch a blog by it’s URL, but you could use the content_by_id function to grab a post by it’s ID number. The ID number can be found in the page editor URL of a post.

33823i172CD53C5E646408.png

But so the user doesn’t have to go find the ID you could use the Page field in a custom module to grab that:

<div class="row-fluid-wrapper">
 <div class="row-fluid">
 {% for postID in module.page_field %}
 {% set post = content_by_id(postID) %}

 <div class="span12">
 <a href="{{post.absolute_url}}" class="blog-post">
 <div class="span6">
 <div class="blog-img-outer">
 <img src="{{ post.post_list_summary_featured_image }}" alt="{{ post.featured_image_alt_text }}">
 </div>
 </div>

 <div class="span6">
 <h3>{{ post.name }}</h3>
 <h3>{{ post.publish_date }}</h3>
 <p>
 {{ post.post_summary|striptags|truncate(150,false,'...') }}
 </p>
 <span class="read-more">Read More</span>
 </div>
 </a>
 </div>
 {% endfor %}
 </div>
</div>

Thank you very much @alyssamwilie! You’ve explain it well also. This was exactly what I wanted.