繁体   English   中英

WordPress-没有category.php的类别页面

[英]WordPress - Category page without category.php

我有一个类别称为test的帖子,我不会显示该类别中的所有帖子。 使用此代码:

$categories = get_categories($args);
    foreach($categories as $category) { 
      echo '<p><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> '; } 
      ?>

所以现在,如果我转到http:// localhost:8888 / category / test /,它将显示该类别的所有帖子。

现在,我的问题是此页面未设置样式。 我调查了wordpress的结构,发现如果我创建了category.php,这将覆盖以前的页面,但失去了显示所选类别帖子的功能。

有没有找到该功能的方法?

非常感谢!

复制category.php页面并将其重命名为category-test.php 然后,您可以使用它来创建自定义类别模板。 https://codex.wordpress.org/Category_Templates

事实证明,复制index.php文件并将其重命名为category.php可以解决问题,将循环保留在index.php中是问题所在

如果您有很多类别,并且想要显示样式或帖子,则可以在category.php添加自定义代码

对于当前类别,可以使用get_the_category()

$categories = get_the_category();
$category_id = $categories[0]->cat_ID; // this will give you current category id

将您的类别ID传递到get_posts函数中

$ posts_array = get_posts(array('category'=> $ category_id));

$posts_array将返回当前类别的所有帖子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM