CMS Development

rossatgrowth
Contributor | Platinum Partner
Contributor | Platinum Partner

Custom Modules on Blog Index

SOLVE

Hello all,

 

We're building out a blog listing and post template for a client. In our mockup, the top section of the index contained a "featured post" that the client can edit the image and content for depending on which post they want to be pinned. However, there doesn't seem to be an option to edit the content on the blog index page.

 

Is this possible? If not, is it possible to display the first (latest) post content in this section, and in the actual index section start at the 2nd most recent post (I imagine this would use a HubL loop/filter of some sort) ?

 

Thanks!

-Ross

0 Upvotes
2 Accepted solutions
albertsg
Solution
Contributeur de premier rang

Custom Modules on Blog Index

SOLVE

Hi @rossatgrowth, yes it is possible!

I believe you have several ways to solve it, here are some ideas:

  • Use blog_recent_posts function to get the latests articles (they are sorted by most recent first).
  • Use a tag (for example #pinned) and blog_recent_tag_posts to get the latest article with this tag.
  • Create a custom module in your blog posts that allows the "pinned/featured" option, so in your listing code you can show the latest post with this option enabled (boolean). 

 

Hope you find this information useful!



Did my answer help you? Mark it as a solution

You also connect with me on LinkedIn or Twitter

View solution in original post

Teun
Solution
Key Advisor | Diamond Partner
Key Advisor | Diamond Partner

Custom Modules on Blog Index

SOLVE

@rossatgrowth the suggestion of @albertsg is correct.

You can only edit the content of modules through the template itself. You could setup an HubDB and add the right fields there for this specific element.


So lets assume you want the newest post, your code could looke something like this:

 

{% if current_page_num == 1 %} // If you only want to show the newest post on the first page of your blog (and have pagination)
 <div class="featured-post-container">
  {% set featured_post = contents[0] %}
  // Add HTML using the contents of featured_post
 </div>
{% endif %}

// Start the default loop here
<div class="posts-container">
 {% for post in contents %}
   {% if not loop.first %}
     // Add HTML using the contents of post
   {% endif %}
 {% endfor %}
</div>

 

 

If you want to show a featured post based on a tag, you could do something like this:

 

{% if current_page_num == 1 %} // If you only want to show the newest post on the first page of your blog (and have pagination)
 <div class="featured-post-container">
  {% set featured_post = blog_recent_tag_posts('default', 'featured', 1 )[0] %}
  // Add HTML using the contents of featured_post
 </div>
{% endif %}

// Start the default loop here
<div class="posts-container">
 {% for post in contents %}
   // Add HTML using the contents of post
 {% endfor %}
</div>

 

Where the second argument of blog_recent_tag_posts should be the name of the tag for your featured post.



Did my answer solve your issue? Help the community by marking it as the solution.

View solution in original post

4 Replies 4
rossatgrowth
Contributor | Platinum Partner
Contributor | Platinum Partner

Custom Modules on Blog Index

SOLVE

Thanks @albertsg and @Teun,

 

Both answers were extremely helpful! I marked each as a solution and thanks @Teun for explaining in further detail with examples (best way to learn!).

 

The tag approach definitely works best for this particular instance, but out of curiosity say I wanted to feature the most recent post at the top of the page and list the rest of the posts in a section below. How would I accomplish the second section by skipping over the first iteration of the for loop so the most recent post is not repeated in both sections?

 

Thanks again,

Ross

0 Upvotes
Teun
Key Advisor | Diamond Partner
Key Advisor | Diamond Partner

Custom Modules on Blog Index

SOLVE

@rossatgrowth
The first code snippet in my comment does just that! The if statement using 'if not loop.first' lets you skip the first iteration.



Did my answer solve your issue? Help the community by marking it as the solution.
Teun
Solution
Key Advisor | Diamond Partner
Key Advisor | Diamond Partner

Custom Modules on Blog Index

SOLVE

@rossatgrowth the suggestion of @albertsg is correct.

You can only edit the content of modules through the template itself. You could setup an HubDB and add the right fields there for this specific element.


So lets assume you want the newest post, your code could looke something like this:

 

{% if current_page_num == 1 %} // If you only want to show the newest post on the first page of your blog (and have pagination)
 <div class="featured-post-container">
  {% set featured_post = contents[0] %}
  // Add HTML using the contents of featured_post
 </div>
{% endif %}

// Start the default loop here
<div class="posts-container">
 {% for post in contents %}
   {% if not loop.first %}
     // Add HTML using the contents of post
   {% endif %}
 {% endfor %}
</div>

 

 

If you want to show a featured post based on a tag, you could do something like this:

 

{% if current_page_num == 1 %} // If you only want to show the newest post on the first page of your blog (and have pagination)
 <div class="featured-post-container">
  {% set featured_post = blog_recent_tag_posts('default', 'featured', 1 )[0] %}
  // Add HTML using the contents of featured_post
 </div>
{% endif %}

// Start the default loop here
<div class="posts-container">
 {% for post in contents %}
   // Add HTML using the contents of post
 {% endfor %}
</div>

 

Where the second argument of blog_recent_tag_posts should be the name of the tag for your featured post.



Did my answer solve your issue? Help the community by marking it as the solution.
albertsg
Solution
Contributeur de premier rang

Custom Modules on Blog Index

SOLVE

Hi @rossatgrowth, yes it is possible!

I believe you have several ways to solve it, here are some ideas:

  • Use blog_recent_posts function to get the latests articles (they are sorted by most recent first).
  • Use a tag (for example #pinned) and blog_recent_tag_posts to get the latest article with this tag.
  • Create a custom module in your blog posts that allows the "pinned/featured" option, so in your listing code you can show the latest post with this option enabled (boolean). 

 

Hope you find this information useful!



Did my answer help you? Mark it as a solution

You also connect with me on LinkedIn or Twitter