简体   繁体   中英

WordPress - Category page without category.php

I have a category for posts called test, which I wan't to display all posts in this category. Using this code:

$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> '; } 
      ?>

so now, if I go to http://localhost:8888/category/test/ it displays all the posts of this category.

Now, my issue is that this page is not styled. I looked into the structure of wordpress, and found that if I made a category.php, this over writes the page before, but looses the functionality of displaying the selected categories posts.

Is there a way to locate this functionality?

Many thanks!

duplicate the category.php page and rename it to category-test.php . Then you can use this to create a custom category template. https://codex.wordpress.org/Category_Templates

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

if you have many categories and want to display your styling or the posts then add your custom code in category.php

for the current category You can use get_the_category()

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

pass your category id into the get_posts function

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

$posts_array will return you all the posts of current category

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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