简体   繁体   中英

Use a single wordpress category.php file for every category registered

As the title suggests, I'm trying to use one category.php file for every category registered by a user in the back-end. I've always created category-[specific category].php files in the past but this feels very messy as the design usually remains the same.

Without being specific in the query I return all posts in all categories however, I want to be able to show the custom posts for only the category selected from the nav. The problem is, I can't seem to find a way to pass the category the user selected into the query in order to only return posts attributed to it.

Thanks in advance for any help.

You don't need to do anything specific to show a list of posts from a specific category. Take a look at this article on the WordPress Codex . Here is an excerpt from that page:

In the case of categories, the hierarchy is fairly simple. For instance, suppose the slug of the Category in question is news and the Category ID is 6. The Template Hierarchy specifies that WordPress will use the first Template file it finds in your current Theme's directory from the following list:

  1. category-slug.php
  2. category-ID.php
  3. category.php
  4. archive.php
  5. index.php

So all you need to do is create category.php in your theme and then view any category and it'll use that template. You can then use is_category() to check which category is currently being used if you want to do custom things depending on the 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