查询 post 和 post 类型,每个类型都有一个模板部分

Query post and post type with a template-part for each

我如何加载 post 和 post 类型(命名为 "Projets"),为每个类型使用不同的模板部分?这里我的查询正在加载 post 和 post 类型,但它们最终都具有相同的设计。 谢谢!

if ( $query->have_posts() )
{
    ?>

    <?php
    while ($query->have_posts())
    {
        $query->the_post();

        ?>
        <?php  get_template_part('template-parts/content', 'content');?>
        <?php
    }
    ?>

未检查但应该有效

为了在循环中获得电流 post_type 使用:

if ( 'the_post_type_here' == get_post_type() ){ code... }

所以你的代码应该是这样的:

if ( $query->have_posts() )
{
?>

<?php
while ($query->have_posts())
{
    $query->the_post();

    ?>
    <?php
       if ( 'Projets' == get_post_type() ){
        get_template_part('template-parts/content', 'content');
       }
    ?>
    <?php
}
?>