CMS Development

benji_thecroc
Colaborador(a) | Parceiro Ouro
Colaborador(a) | Parceiro Ouro

Exclude current blog post from recent posts

resolver

Hi,

 

I'm using this code snippet here to pull the three most recent blog posts on my blog post template. However I would like to adjust this functionality to exclude a blog post if it's the one currently being viewed.

 

Is this acheivable?

 

Thanks,

@benji_thecroc 

Benji

 

0 Avaliação positiva
1 Solução aceita
JasonRosa
Solução
HubSpot Employee
HubSpot Employee

Exclude current blog post from recent posts

resolver

Hey, @benji_thecroc thank you for the response! That makes plenty of sense. Could you try something like this instead: 

 

{% set rec_posts = blog_recent_posts('default', 5) %}
  {% set i = 0 %}
  {% for rec_post in rec_posts %}
    {% if content.absolute_url != rec_post.absolute_url %}
      {% if i > 4 %}
        <div class="post-title">{{ rec_post.name }}</div>
        {% set i = i + 1 %}
      {% endif %}
    {% endif %}
  {% endfor %}

This should pull in the 5 most recent posts (in case the most recent post is the one you're currently viewing) and then show the first 4 that meet your criteria. 

Exibir solução no post original

6 Respostas 6
jzilch
HubSpot Employee
HubSpot Employee

Exclude current blog post from recent posts

resolver

For anyone else looking for a solution down the line I have another example below. 

 

Thanks Jason!

 

<h3>Latest posts</h3>
  <div class="cm-recent-blog-wrapper">
    {% set max_posts = 3 %}<!-- Set the max number of recent posts to be output to the page here -->
    {% set rec_posts = [] %}
    {% set rec_posts = rec_posts + blog_recent_posts('group.id', max_posts + 1) %}
    {% set i = 0 %}
      {% for post in rec_posts %}
        {% if content.absolute_url != post.absolute_url and i < max_posts %}
        <div class="recent-post-item">
          <a href="{{ post.absolute_url }}">
            {% if post.post_list_summary_featured_image %}
            <div class="recent-hs-featured-image-wrapper">
              <img src="{{ resize_image_url( post.post_list_summary_featured_image,767 ) }}" class="recent-hs-featured-image" alt="{{ post.featured_image_alt_text | escape }}">
            </div>
            {% endif %}
            <div class="recent-post-title">{{ post.name}}</div>
          </a>
        </div>
        {% set i = i + 1 %}          
        {% endif %}
      {% endfor %}
  </div>
</div>
JasonRosa
HubSpot Employee
HubSpot Employee

Exclude current blog post from recent posts

resolver

@benji_thecroc I'd recommend using an HubL if statement to see if your current URL is equal to the URL in your loop. So something like this (if statement on line 3): 

 

{% set rec_posts = blog_recent_posts('default', 4) %}
  {% for rec_post in rec_posts %}
    {% if content.absolute_url != rec_post.absolute_url %}
      <div class="post-title">{{ rec_post.name }}</div>
    {% endif %}
  {% endfor %}

 

0 Avaliação positiva
benji_thecroc
Colaborador(a) | Parceiro Ouro
Colaborador(a) | Parceiro Ouro

Exclude current blog post from recent posts

resolver

Hi @JasonRosa 

 

This is helpful however this would result in only 3 recent posts being displayed instead of 4.

 

Ideally I'd want to exclude the current post from the 'blog_recent_posts' query and still pull in 4 blog posts rather than include current post in query but just not display it.

 

For example in a WordPress query within the argument array you can set something like -

 

<?php
$currentID = get_the_ID();
$my_query = new WP_Query( array('showposts' => '4', 'post__not_in' => array($currentID)));
while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title() ?></a></h2>
<?php the_content(); ?> <?php endwhile; ?>

 This picks up current post ID and excludes said post from the query results while retaining 4 posts in the loop.

0 Avaliação positiva
JasonRosa
Solução
HubSpot Employee
HubSpot Employee

Exclude current blog post from recent posts

resolver

Hey, @benji_thecroc thank you for the response! That makes plenty of sense. Could you try something like this instead: 

 

{% set rec_posts = blog_recent_posts('default', 5) %}
  {% set i = 0 %}
  {% for rec_post in rec_posts %}
    {% if content.absolute_url != rec_post.absolute_url %}
      {% if i > 4 %}
        <div class="post-title">{{ rec_post.name }}</div>
        {% set i = i + 1 %}
      {% endif %}
    {% endif %}
  {% endfor %}

This should pull in the 5 most recent posts (in case the most recent post is the one you're currently viewing) and then show the first 4 that meet your criteria. 

benji_thecroc
Colaborador(a) | Parceiro Ouro
Colaborador(a) | Parceiro Ouro

Exclude current blog post from recent posts

resolver

That did the job, thank you for the solution!

0 Avaliação positiva
JasonRosa
HubSpot Employee
HubSpot Employee

Exclude current blog post from recent posts

resolver

You're very welcome!

0 Avaliação positiva