Search code examples
wordpresspostcustom-post-type

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


How can I load post and post type (named "Projets") using différent template-parts for each one of them ? Here My query is loading post and post type, but both of them have the same design in the end. Thank you !

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

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

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

Solution

  • Not checked but it supposed to work

    In order to get current post_type while in the loop use:

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

    so your code shoud go like:

    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
    }
    ?>