CMS Development

shiyon
Contributor

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

SOLVE

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

 

Clipboard01.jpg

0 Upvotes
1 Accepted solution
alyssamwilie
Solution
Recognized Expert | Elite Partner
Recognized Expert | Elite Partner

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

SOLVE

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.

postid.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:

page-field.png

<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>

 

If this answer solved your question, please mark it as the solution.

Alyssa Wilie Profile Image

Alyssa Wilie

Web Developerat Lynton

Learn HubL | Get Marketing Insights

HubSpot Elite Solutions Partner
Lynton's HubSpot theme Rubric now available. Click to download.

View solution in original post

2 Replies 2
alyssamwilie
Solution
Recognized Expert | Elite Partner
Recognized Expert | Elite Partner

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

SOLVE

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.

postid.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:

page-field.png

<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>

 

If this answer solved your question, please mark it as the solution.

Alyssa Wilie Profile Image

Alyssa Wilie

Web Developerat Lynton

Learn HubL | Get Marketing Insights

HubSpot Elite Solutions Partner
Lynton's HubSpot theme Rubric now available. Click to download.
shiyon
Contributor

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

SOLVE

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

0 Upvotes