nikolaou,
When I understand you question correct then you want to use different templates based on the categories you’re using within your posts, correct?
If that’s the case, then you only need one template (single.php) and a combination of get_the_terms()
and get_template_part()
.
<?php
$categories = get_the_terms(get_the_ID(), 'slug');
if ( in_array('cat1', $categories) ) {
get_template_part('partials/single', '1');
} elseif ( in_array('cat2', $categories) ) {
get_template_part('partials/single', '2');
}
?>
You should place the code above within The Loop. The two template files are called single-1.php and single-2.php and stored within a folder called partials.
Sources:
Related Posts:
- How to get Images included in Post
- Use template of parent category for single post sub-categories
- How only display all post related to category
- Different template for posts of all subcategories of category
- Mark menu item as current-menu-item for category
- Category Template: Need to display different content on first page of archives
- Exclude category from
- Disable sticky option for specific categories
- How to create a template for Pages?
- How do I stop the loop from repeating in my category template?
- how to hide empty fields of post category description?
- Which hooks are essential for post templates?
- Display the first post’s comments of category in comments.php template
- how to style an individual page in a category [duplicate]
- Change from all posts to specific categories post on main page?
- excerpt in template for specific page
- Is there a block to print post link standalone in a block theme?
- MySQL Query to Retrieve Category from wp_posts
- Category checkbox list tree changes when editing a post
- How to sort posts by last name (2nd word) on ONE category only?
- Single.php Active Category Class
- Most efficient way to list all categories and display a post for each of them?
- prevent showing posts of an specific category in admin posts section
- How to Replace the WordPress Featured Image with a Video?
- Auto set Post to Specific Categories
- New blog template to display only one category
- Is it possible to have two different previews of a post (ie. two templates for one post)?
- WordPress menu that shows all posts in a certain category like w3schools
- How can I list random authors from current post category?
- How do I render all posts of the same category in same layout?
- Custom Post Templates
- How to limit displayed posts on wordpress
- WordPress list posts from sub categories
- Templating of a specific post ID
- Select a random post from a specific category?
- How to filter posts that belong to a specific category only if that is the only category
- Category List (Archive) Page Template By Category
- List posts in a given category
- Why orderby=”date” does not support manually changed posts?
- page.php showing loop of posts and not page content
- Child_of not displaying all children posts.. via get_posts
- post categories
- How to display the link (title) and thumbnail post?
- How can i limit the number of posts created per category?
- Get Posts Under Custom Taxonomy
- Prevent additional top level categories but allow more children
- How can I hide tags on a child-category page, if that tag has not been used?
- Is it possible to add/tick a category to a post when it is created?
- Hide posts belongs to few categories in homepage
- post category in wp_insert_post
- posts not showing on index.php
- What does WordPress do if I save a post without content/title? [duplicate]
- store posts_id of category into a varable?
- Display posts of specific category term
- Widget that shows categories with posts numbers
- Latest posts by category — how to exclude current post?
- Relative number of post in category
- How to get post with associated categories and tags names instead of ids with rest api?
- Force a specific template as default
- Display a mixed list of posts and media
- Un-highlight Blog Menu Item when Category Menu Item is Selected
- Change the background of each post according to the category
- Programmatically add posts from specific category to menu
- Get post category as a separate string and url
- How do I disable the “by author” hyperlink on posts?
- How to show category based post in home page?
- I can’t find the relevant part of the loop in my theme file. How do I find the full content with “More…”
- add_rewrite_rule and template redirect not working
- How to view all posts (on site, not admin) that are uncategorized?
- How to create a list of links to post with specific category
- Permanently deleted posts still exist
- Post meta not working
- get category in list of posts // shortcode for custom related posts
- Related post not showing table of content
- Featured Story Shortcode not outputting content
- Post template not applying on theme (potentially rendering as Page template)
- How to add a ‘News’ section to specific posts in WordPress
- Get Category in save_post Hook
- How to prevent clicking on posts
- Single Sidebar; multiple templates! How?
- How to show category image if no featured image is set?
- How to run select query of post with category and tags?
- Custom post type template not called
- Page 2 has no posts?
- Point users to a specific url directory for some posts
- Static text above category page
- Show only top 3 posts from 3 categories in order on home page
- Hide categories from showing above a Post
- Adding The Post Template Dropdown to WP-Admin
- Order posts by separate menu order for different sub-categories
- Delete old post with new post
- I need to create a page or post hierarchy
- Separate categories parents, childs and grandchilds in dropdowns
- 1 post, 2 templates
- Exclude sub sub categories from displaying in the archive page
- Taxonomy Category category.php not working
- How to sort the post to a specific category
- View post with specific category id and name which I selected in the backend (drop-down option)
- How can I fixe the article title problem?
- Automatically select category based on user role