【问题标题】:WordPress; Display different content template based on categoryWordPress;根据类别显示不同的内容模板
【发布时间】:2019-02-17 22:46:13
【问题描述】:

我想采用基本的 WordPress 循环,并在它调用内容模板的地方,我想运行一个 if else 语句,该语句根据类别拉入不同的模板部分。我知道我可以制作一堆archive.php 的副本,但是为什么要为您想要使用的每个类别都这样做呢?

所以我尝试了这段代码,但它只是在每种情况下都拉入了存档模板:

while ( have_posts() ) :
            the_post();

        if ( is_category( 'my-category-slug' )) {
            get_template_part( 'template-parts/content', 'single' );
        } else {
            get_template_part( 'template-parts/content', 'archive' );
        }

endwhile;

那么我什至可以在循环中运行这样的 if else 语句吗?

【问题讨论】:

    标签: php wordpress if-statement while-loop


    【解决方案1】:

    Wordpress 有一个template hierarchy,你需要确保你的代码在正确的 php 文件中。检查this link。在您的情况下,您需要修改 category.php,如果您删除所有 category*.php 文件,您可以使用 archive.php。

    【讨论】:

    • 本主题中没有 category.php 文件。只有archive.php。它在正确的文件中。
    • 如果它正在执行并且你每次只得到'template-parts/content-archive.php'而从来没有'template-parts/content-single.php'并不意味着您没有包含“my-category-slug”类别的存档页面?或者你没有循环好结果(可能没有使用 WP_Query 和 args )?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    • 2013-05-09
    • 1970-01-01
    • 2018-10-08
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    相关资源
    最近更新 更多