I’m using ACF and I would like to display posts in a different way according to the category. Here is my single :

<?php get_header(); ?>
<?php if(!function_exists('get_field')) return; ?>

<main class="main_flex">
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); 
        if ( in_category('dossiers') ) { get_template_part( 'single_dossier' );
        } else { get_template_part( 'template-parts/content', 'single' );
        } ?>
    <article>
    <!-- content here -->
    </article>
    <?php endwhile; ?>
    <?php endif; ?>
</main>
<?php get_footer(); ?>

And in a file I named single_dossier.php, I wrote this :

<?php if(!function_exists('get_field')) return; ?>

<article>
    <!-- different content here -->
</article>

When I go to an article with the category “dossiers”, it loads both the contents. Anyone could tell me what I did wrong and how to correct it?
Thanks!

Read more here: WordPress get template part with if statement shows both templates


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: