CMS Development

CFrench
Participant

Using blog_recent_topic_posts() to Exclude Current Post in Returned Results

Résolue

Hi,

 

I created a recent posts module for the bottom of my blog to pull in the three most recent posts from the same topic as the present post.  I like the way it's working, but... the first post is the current post.

So is there a way to add in an offset to take posts #2, #3 and #4 and/or exlcude the current post?

Here's the gist of the module:

{% set number_of_posts = 3 %}
{% set posts = blog_recent_topic_posts("default", content.topic_list[0].slug, 3) %}
{% if module.specific_tag_topic %}
	{% set posts = blog_recent_topic_posts(module.choose_blog, module.tag, number_of_posts) %}
{% endif %}

<section class="blog-feed">
  <div>
    <h2>
      Related Reading
    </h2>
  </div>
  <div class="blog-posts">
    {% for post in posts %}
    <div class="blog-post">
      <div class="ni-post blog-post-inner">
        <div class="blog-post-image" style="background-image: url({{ resize_image_url( post.featured_image,0,350 ) }});"></div>
        <div class="blog-post-content-wrapper">
          <a class="blog-post-title" href="{{ post.absolute_url }}"><h3>{{ post.name }}</h3></a>
          <a class="blog-post-read-more" href="{{ post.absolute_url }}">Read More &rarr;</a>
        </div>
      </div>
    </div>
    {% endfor %}
  </div>
</section>
0 Votes
1 Solution acceptée
erod
Solution
Contributeur

Using blog_recent_topic_posts() to Exclude Current Post in Returned Results

Résolue

Similar to what @Teun said but instead of doing content.name you do a range. Below should work.

{% set number_of_posts = 3 %}
{% set posts = blog_recent_topic_posts("default", content.topic_list[0].slug, 4) %}
{% if module.specific_tag_topic %}
	{% set posts = blog_recent_topic_posts(module.choose_blog, module.tag, number_of_posts) %}
{% endif %}

<section class="blog-feed">
  <div>
    <h2>
      Related Reading
    </h2><br>
  </div>
  <div class="blog-posts">
    {% for post in posts %}
    {% if loop.index in range(2,99) %}
    <div class="blog-post">
      <div class="ni-post blog-post-inner">
        <div class="blog-post-image" style="background-image: url({{ resize_image_url( post.featured_image,0,350 ) }});"></div>
        <div class="blog-post-content-wrapper">
          <a class="blog-post-title" href="{{ post.absolute_url }}"><h3>{{ post.name }}</h3></a>
          <a class="blog-post-read-more" href="{{ post.absolute_url }}">Read More &rarr;</a>
        </div>
      </div>
    </div>
    {% endif %}
    {% endfor %}
  </div>
</section>

 

Voir la solution dans l'envoi d'origine

0 Votes
2 Réponses
erod
Solution
Contributeur

Using blog_recent_topic_posts() to Exclude Current Post in Returned Results

Résolue

Similar to what @Teun said but instead of doing content.name you do a range. Below should work.

{% set number_of_posts = 3 %}
{% set posts = blog_recent_topic_posts("default", content.topic_list[0].slug, 4) %}
{% if module.specific_tag_topic %}
	{% set posts = blog_recent_topic_posts(module.choose_blog, module.tag, number_of_posts) %}
{% endif %}

<section class="blog-feed">
  <div>
    <h2>
      Related Reading
    </h2><br>
  </div>
  <div class="blog-posts">
    {% for post in posts %}
    {% if loop.index in range(2,99) %}
    <div class="blog-post">
      <div class="ni-post blog-post-inner">
        <div class="blog-post-image" style="background-image: url({{ resize_image_url( post.featured_image,0,350 ) }});"></div>
        <div class="blog-post-content-wrapper">
          <a class="blog-post-title" href="{{ post.absolute_url }}"><h3>{{ post.name }}</h3></a>
          <a class="blog-post-read-more" href="{{ post.absolute_url }}">Read More &rarr;</a>
        </div>
      </div>
    </div>
    {% endif %}
    {% endfor %}
  </div>
</section>

 

0 Votes
Teun
Expert reconnu | Partenaire solutions Diamond
Expert reconnu | Partenaire solutions Diamond

Using blog_recent_topic_posts() to Exclude Current Post in Returned Results

Résolue

If you fetch 4 posts instead of 3, you can use an if statement to filter out the current post using the name of the post.

 

{% if post.name != content.name %}



Learn more about HubSpot by following me on LinkedIn or YouTube

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


0 Votes