Wordpress获取模板部分,if语句显示两个模板

时间:2017-05-20 00:51:14

标签: wordpress

我正在使用ACF,我想根据类别以不同的方式显示帖子。这是我的单身:

<?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(); ?>

在我命名为single_dossier.php的文件中,我写了这个:

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

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

当我查看带有“档案”类别的文章时,它会加载两个内容。任何人都可以告诉我我做错了什么以及如何纠正它? 谢谢!

1 个答案:

答案 0 :(得分:0)

也许你有2个帖子具有相同的id / slug(取决于你的永久链接结构)。

如需检查,请使用以下代码:

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

<main class="main_flex">
    <?php if (have_posts()) : ?>
    <?php $x = 0; ?>
    <?php while (have_posts()) : the_post();
        $x++;
        echo '{{' . $x . ':' . $post->ID . '}}';
        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(); ?>

让我们看看你是否输入了输出{{1:someID}}&amp; {{2:someAnotherID}}

或使用以下代码:

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

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