Roots Discourse

Why did i get page instead of post in The loop

Hi everyone

I’ve started to build a theme with bedrock & sage.
My theme is soon finished and i discover a lot of things that i lot on sage.

But i need somes posts on my homepage and i can’t get the post thumbnail & excerpt.

The title look like the title of the page (homepage), not the title of the post

Here is my code :

  @if(have_posts())
      @while(have_posts()) @php the_post() @endphp
        <div class="col-md-4 mt-4">
          <article class="post-list-item">
            {!! the_post_thumbnail('full') !!}
            <h3 class="h4">{!! get_the_title() !!}</h3>
            <small class="blog-post-meta">Publié le {!! the_date() !!}</a></small>
            <p>{!! get_the_excerpt() !!}<p>
          </article>
        </div>
      @endwhile
  @endif 

If someone have an idea.

By the way, i already added :
add_theme_support('post-thumbnail');
to my setup.php after the ‘title-tag’ line.

Thank’s you.

That’s not Sage specific. You’re currently on the homepage’s query which will give you its title, thumbnail, …etc. That’s of course unless you you’ve set your homepage to display latest posts.

If you need to grab blog posts or other custom post type, you’ll need to create a custom query for them as you can see here.

What’s related to sage and will make your life easier is Sage Directives.

Working with the WP_Query.

Thank you, i was thinking wp_query is for custom query not for simple post. But now i know. Thank’s you. Here is the code actually if you got the same problem :

  <?php
  $query = new WP_Query([
    'post-type' => 'post'
  ])
  ?> 

  @while ($query->have_posts()) @php $query->the_post() @endphp
    <div class="col-md-4 mt-4">
      <article class="post-list-item">
        {!! the_post_thumbnail('full') !!}
        <h3 class="h4">{!! get_the_title() !!}</h3>
        <small class="blog-post-meta">Publié le {!! the_date() !!}</a></small>
        <p>{!! get_the_excerpt() !!}<p>
      </article>
    </div>
  @endwhile