'How to custom post type in wordpress different style
I want to customize the posts for each category to be different. but I can't do it, can you please take a look at me, where is my mistake?
For example, I have 2 categories.
1 window 2 android
I want the windows category to call the content-windows-details.php file.
android content-app-details.php
But now the category Windows will call the app-details.php file. i need to windown call windows-details.php
Code single.php
<?php
/**
* The template for displaying all single posts
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post
*
*
*/
get_header();
$p_id = get_the_ID();
$terms = get_the_terms( $p_id, 'category' );
$is_tip = false;
$cat_id = 0;
foreach($terms as $term) {
if( !is_app_cat($term->term_id)){
$is_tip = true;
$cat_id = $term->term_id;
break;
}
else{
if($term->parent != 0){
$cat_id = $term->term_id;
}
}
}
if($is_tip){
$is_topten = false;
foreach($terms as $term) {
if($term->slug == 'windows'){
$is_topten = true;
break;
}
}
if($is_topten){
get_template_part( 'template-parts/content', 'windows-details' );
}
else{
get_template_part( 'template-parts/content', 'single' );
}
}
else{
get_template_part( 'template-parts/content', 'app-details' );
}
?>
<?php
get_footer();
Solution 1:[1]
WordPress has built-in support for this kind of thing (I believe you are using WordPress). You just need to rename your files.
Instead of content-windoes-details.php, use category-windows.php
Same thing for Android; it's category-android.php
That way, the correct file will be used when displaying posts from each category.
The Codex page about this is here: https://developer.wordpress.org/themes/basics/template-hierarchy/#examples
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Matt Bedford |
