CMS Development

Lourdes14
Participant | Diamond Partner
Participant | Diamond Partner

Related articule

SOLVE

Hello! Under my posts i have the section about related articules, but they seem to be random and i want to have a relation between the main post and the related post. I dont know if it is posibble to relate them by tag or topic.

 

Now i have this code: 

 

<div class="recent-posts">
{% if content.topic_list %}
{% for topic in content.topic_list %}
{% if loop.first %}
{% set counter = 0 %}
{% set related_posts = blog_recent_topic_posts('default', topic.slug, 4 ) %}
{% for post in related_posts %}
{% unless content.absolute_url == post.absolute_url %}
{% set counter = counter + 1 %}
{% if counter < 4 %}
<div class="recent-post">
<a href="{{ post.absolute_url }}">
<div class="recent-post-featured-image" style="background-image: url('{{ post.featured_image }}')"></div>
</a>
<div class="recent-post-content">
<p class="recent-post-info">
<i style="color:#1883b3;" class="fa fa-calendar"></i>
{{ post.publish_date|datetimeformat('%d %B, %Y') }} &nbsp;&nbsp;&nbsp;
<i class="fa fa-tags" style="color:#1883b3;"></i>
<a class="topic-related" href="{{ group.absolute_url }}/topic/{{ content.topic_list[0].slug }}">
{{post.topic_list[0].name }}
</a>
</p>
<a href="{{ post.absolute_url }}">
<h4 class="recent-post-title">{{ post.name }}</h4>
<p class="recent-post-read-more hs-button primary" href="{{ post.absolute_url }}">Seguir leyendo</p>
</a>
</div>
</div>
{% endif %}
{% endunless %}
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}
</div>

0 Upvotes
1 Accepted solution
piersg
Solution
Key Advisor

Related articule

SOLVE

Sure

{# Posts relacionados (Old) #}
<h3 class="title-comment">Posts relacionados</h3>
<div class="recent-posts">
  {% set rec_posts = blog_recent_tag_posts('default',content.tag_list[0].slug, 4) %}
  {% set loop = 0 %}
  {% for post in rec_posts %}
    {% unless post.id == content.id %}
      {% set loop = loop + 1 %}
      {% if loop < 4 %}
        <div class="recent-post">
          <a href="{{ post.absolute_url }}">
             <div class="recent-post-featured-image" style="background-image: url('{{ post.featured_image }}')"></div>
          </a>
          <div class="recent-post-content">
            <p class="recent-post-info">
              <i style="color:#1883b3;" class="fa fa-calendar"></i>
               {{ post.publish_date|datetimeformat('%d %B, %Y') }} &nbsp;&nbsp;&nbsp;
              <i class="fa fa-tags" style="color:#1883b3;"></i>
              <a class="topic-related" href="{{ group.absolute_url }}/topic/{{ content.topic_list[0].slug }}">
                {{post.topic_list[0].name }}
              </a>
            </p>
            <a href="{{ post.absolute_url }}">
            <h4 class="recent-post-title">{{ post.name }}</h4>
            <p class="recent-post-read-more hs-button primary" href="{{ post.absolute_url }}">Seguir leyendo</p>
            </a>
          </div>
        </div>
      {% endif %}
    {% endunless %}
  {% endfor %}
</div>

View solution in original post

3 Replies 3
piersg
Solution
Key Advisor

Related articule

SOLVE

Sure

{# Posts relacionados (Old) #}
<h3 class="title-comment">Posts relacionados</h3>
<div class="recent-posts">
  {% set rec_posts = blog_recent_tag_posts('default',content.tag_list[0].slug, 4) %}
  {% set loop = 0 %}
  {% for post in rec_posts %}
    {% unless post.id == content.id %}
      {% set loop = loop + 1 %}
      {% if loop < 4 %}
        <div class="recent-post">
          <a href="{{ post.absolute_url }}">
             <div class="recent-post-featured-image" style="background-image: url('{{ post.featured_image }}')"></div>
          </a>
          <div class="recent-post-content">
            <p class="recent-post-info">
              <i style="color:#1883b3;" class="fa fa-calendar"></i>
               {{ post.publish_date|datetimeformat('%d %B, %Y') }} &nbsp;&nbsp;&nbsp;
              <i class="fa fa-tags" style="color:#1883b3;"></i>
              <a class="topic-related" href="{{ group.absolute_url }}/topic/{{ content.topic_list[0].slug }}">
                {{post.topic_list[0].name }}
              </a>
            </p>
            <a href="{{ post.absolute_url }}">
            <h4 class="recent-post-title">{{ post.name }}</h4>
            <p class="recent-post-read-more hs-button primary" href="{{ post.absolute_url }}">Seguir leyendo</p>
            </a>
          </div>
        </div>
      {% endif %}
    {% endunless %}
  {% endfor %}
</div>
Lourdes14
Participant | Diamond Partner
Participant | Diamond Partner

Related articule

SOLVE

Hi @piersg!! Thanks for your help but it still does not work, I have tried to replace with the piece of code that you have passed me but it still does not work, could you modify the code of this comment with the code that you tell me to see if I am doing something wrong? Thanks for your time 🙂

 

{# Posts relacionados (Old) #}
<h3 class="title-comment">Posts relacionados</h3>
<div class="recent-posts">
    {% if content.topic_list %}
      {% for topic in content.topic_list %}
        {% if loop.first %}
          {% set counter = 0 %}
          {% set related_posts = blog_recent_topic_posts('default', topic.slug, 4 ) %}
          {% for post in related_posts %}
            {% unless content.absolute_url == post.absolute_url %}
              {% set counter = counter + 1 %}
              {% if counter < 4 %}
                <div class="recent-post">
                  <a href="{{ post.absolute_url }}">
                     <div class="recent-post-featured-image" style="background-image: url('{{ post.featured_image }}')"></div>
                 </a>
                 <div class="recent-post-content">
                   <p class="recent-post-info">
                     <i style="color:#1883b3;" class="fa fa-calendar"></i>
                     {{ post.publish_date|datetimeformat('%d %B, %Y') }} &nbsp;&nbsp;&nbsp;
                     <i class="fa fa-tags" style="color:#1883b3;"></i>
                    <a class="topic-related" href="{{ group.absolute_url }}/topic/{{ content.topic_list[0].slug }}">
                       {{post.topic_list[0].name }}
                    </a>
                </p>
               <a href="{{ post.absolute_url }}">
                <h4 class="recent-post-title">{{ post.name }}</h4>
                <p class="recent-post-read-more hs-button primary" href="{{ post.absolute_url }}">Seguir leyendo</p>
              </a>
           </div>
       </div>
      {% endif %}
     {% endunless %}
   {% endfor %}
  {% endif %}
 {% endfor %}
{% endif %}
</div>

0 Upvotes
piersg
Key Advisor

Related articule

SOLVE

Hi @Lourdes14, here's my HubL for getting recent posts with the same tag/topic as the blog article being read (not including the one being read):

{% set rec_posts = blog_recent_tag_posts('default',content.tag_list[0].slug, 4) %}
{% set loop = 0 %}
{% for rec_post in rec_posts %}
  {% unless rec_post.id == content.id %}
    {% set loop = loop + 1 %}
    {% if loop < 3 %}
    // recent/similar post code here

    {% endif %}
  {% endunless %}
{% endfor %}