类别段塞不工作


Category-slug not working

我正在开发一个报价网站,因为我想要每个类别的不同类别页面模板页面,我遵循了wordpress:的链接

https://codex.wordpress.org/Category_Templates

根据这一点,我制作了不同类别的流行author.php(也尝试了类别-7.php bt不起作用),而不是我原来的category.php文件,但不起作用。我还尝试了一些其他选项,比如:

1) 在中尝试过此代码

functions.php
function wpse_category_single_template( $single_template ) {
    global $post;
    $all_cats = get_the_category();
    if ( $all_cats[0]->cat_ID == '1' ) {
        if ( file_exists(get_template_directory() . "/single-cat1.php") )           return get_template_directory() . "/single-cat1.php";
    } elseif ( $all_cats[0]->cat_ID == '2' ) {
        if ( file_exists(get_template_directory() . "/single-cat2.php") ) return get_template_directory() . "/single-cat2.php";
    }
    return $single_template;
}
add_filter( 'single_template', 'wpse_category_single_template' );

但没有工作

2) 改变了我的永久链接结构。

3) 尝试将if...else条件放入category.php文件中,如下所示:

<?php if (is_category('ID')) : ?>
// Code For Category with defined ID
<?php else : ?>
// Code for else
<?php endif; ?>

但仍然没有进展。

实际上wordpress找不到我的category-slug.php文件

怎么了?

您的文件名应该是category-popular-author.php,而不是category-popular author.php。你的破折号不见了。

如果您的类别ID是7,那么我猜您的文件在错误的目录中。因为category-7.php应该起作用。

来自codex。

模板层次结构指定WordPress将使用它在当前主题的目录中从以下列表:

  • category-plug.php
  • 类别ID.php
  • 类别.php
  • archive.php
  • index.php

所以category-popular-author.php应该在wp-content/themes/CURRENT_THEME_NAME/category-popular-author.php