CMS Development

CFrench
Participant

Using blog_recent_topic_posts() to Exclude Current Post in Returned Results

SOLVE

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 Upvotes
1 Accepted solution
erod
Solution
Contributor

Using blog_recent_topic_posts() to Exclude Current Post in Returned Results

SOLVE

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>

 

View solution in original post

0 Upvotes
2 Replies 2
erod
Solution
Contributor

Using blog_recent_topic_posts() to Exclude Current Post in Returned Results

SOLVE

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 Upvotes
Teun
Recognized Expert | Diamond Partner
Recognized Expert | Diamond Partner

Using blog_recent_topic_posts() to Exclude Current Post in Returned Results

SOLVE

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



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