最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Custom template page with custom archives listing by user

programmeradmin2浏览0评论

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.

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.

Share Improve this question edited Mar 26, 2020 at 15:10 WordPress Speed 2,2833 gold badges19 silver badges34 bronze badges asked Mar 26, 2020 at 10:26 MorganMorgan 13710 bronze badges 2
  • 1 remove that extra the_post(); after $the_query->the_post();. – WordPress Speed Commented Mar 26, 2020 at 13:50
  • That's it ! don't know, why, didn't work the first time :/ – Morgan Commented Mar 26, 2020 at 14:09
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论