我想为每个类别定制不同的帖子。但是我做不到,请你看看我,我的错误在哪里?
例如,我有两个类别。
1 window 2 android
我希望windows类别调用内容-windows-细节s.php文件。
android内容-app-细节s.php
但现在,Windows类别将调用app-细节s.php文件。我需要关闭调用windows-diets.php
代码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();
发布于 2022-04-20 05:45:06
WordPress已经内置了对这类东西的支持(我相信您正在使用WordPress)。你只需要重命名你的文件。
而不是使用category-windows.php,而不是内容-windoes细节s.php
安卓系统也是如此,它是category-android.php
这样,在显示每个类别的帖子时,将使用正确的文件。
关于这方面的法典页是:https://developer.wordpress.org/themes/basics/template-hierarchy/#examples
https://stackoverflow.com/questions/71933788
复制相似问题