CMS Development

kchung
Member

Dynamic blog list based on specific topics (tags)

SOLVE

I used this code to dynamically list 5 most recent blogs. It includes an image and title of the blog - both are linked (and it works great):

 

<div class="dynamic-blog-item">{% set posts = blog_recent_posts('default', 5) %} {% for post in posts %}
<div class="dynamic-blog-content">
<div class="dynamic-blog-image"><a href="{{ post.absolute_url }}"><img src="{{ post.featured_image }}" alt="{{ post.name }}"></a></div>
<div class="dynamic-blog-title">
<h3><a href="{{ post.absolute_url }}">{{ post.name }}</a></h3>
</div>
</div>
{% endfor %}

</div>

 

I would like to use the same structure above, but this time dynamically list the most recent blog by one specific topic (tag).

 

I believe you can use: blog_recent_tag_posts  -- but I was not able to make it work. Any help is greatly appreciated - I am not a developer so please explain in detail. Thanks.

0 Upvotes
1 Accepted solution
albertsg
Solution
Guide

Dynamic blog list based on specific topics (tags)

SOLVE

Hello @kchung, as you say, you can use blog_recent_tag_posts 🙂

 

Here's an example of how to use it in your code:

 

<div class="dynamic-blog-item">
{% set articles_tag = blog_recent_tag_posts('default', 'my_tag_name', 5 ) %}
    {% for post in articles_tag %}
        <div class="dynamic-blog-content">
            <div class="dynamic-blog-image">
                <a href="{{ post.absolute_url }}"><img src="{{ post.featured_image }}" alt="{{ post.name }}"></a>
            </div>
            <div class="dynamic-blog-title">
                <h3><a href="{{ post.absolute_url }}">{{ post.name }}</a></h3>
            </div>
        </div>
    {% endfor %}
</div>

 

 

Just remember to replace the string 'my_tag_name' for your tag-slug (notice that tag-slug is not the same as tag name, for example a tag that has "my tag" as name will have "my-tag" as slug).



Did my answer help you? Mark it as a solution

View solution in original post

1 Reply 1
albertsg
Solution
Guide

Dynamic blog list based on specific topics (tags)

SOLVE

Hello @kchung, as you say, you can use blog_recent_tag_posts 🙂

 

Here's an example of how to use it in your code:

 

<div class="dynamic-blog-item">
{% set articles_tag = blog_recent_tag_posts('default', 'my_tag_name', 5 ) %}
    {% for post in articles_tag %}
        <div class="dynamic-blog-content">
            <div class="dynamic-blog-image">
                <a href="{{ post.absolute_url }}"><img src="{{ post.featured_image }}" alt="{{ post.name }}"></a>
            </div>
            <div class="dynamic-blog-title">
                <h3><a href="{{ post.absolute_url }}">{{ post.name }}</a></h3>
            </div>
        </div>
    {% endfor %}
</div>

 

 

Just remember to replace the string 'my_tag_name' for your tag-slug (notice that tag-slug is not the same as tag name, for example a tag that has "my tag" as name will have "my-tag" as slug).



Did my answer help you? Mark it as a solution