1

I'm having an archive page for my post type. It's working, I'm using the template part with the get_post_type() parameters.

content-activite.php:

<?php
$post_id = get_the_ID();
$post_thumbnail = get_the_post_thumbnail();
$attachment_id = get_post_thumbnail_id($post_id);
$attachment_meta = wp_get_attachment($attachment_id);
?>

<div class="col-12 col-md-6 col-xl-3 mb-4">
    <div class="card mb-4">
        <a href="<?php the_permalink(); ?>">
            <img class="card-img-top" src="<?php echo $attachment_meta['src']; ?>"
                 alt="<?php echo $attachment_meta['alt']; ?>">
            <div class="card-body flex-fill">
                <h5 class="card-title"><?php the_title(); ?></h5>
            </div>
            <div class="card-footer">
                <p class="m-0">
                </p>
            </div>
        </a>
    </div>
</div>

I need to create the same layout with the same archive but for my current user. So I've created my page, assign a template to this and create my custom query.

But when I use this ($post_type = get_post_type()):

if ($the_query->have_posts()) :
    while ($the_query->have_posts()) :
        $the_query->the_post();
        the_post();
        get_template_part('template-parts/content', $post_type);
    endwhile;
endif;

the_title(); is getting the name of the page, and not the name of my post. How can I get the name of my post?

PS:

I will need to change some data displayed on the template-part, but use one file only for the activities of my user and my archives.

  • 1
    remove that extra the_post(); after $the_query->the_post();. – Pmpr Mar 26 at 13:50
  • That's it ! don't know, why, didn't work the first time :/ – Morgan Tartreau Mar 26 at 14:09
1

Consider removing that extra the_post(); after $the_query->the_post();.

That will solve the problem. You may just use that once on the Query object. You have a custom quesry, then just call it on that exact object is required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.