这个问题是为了对WordPress模板文件的使用进行非常具体的细化。细化的工作方式类似于默认的archive.php和变体的工作方式,但我们希望有第二个模板,它具有通过链接地址查询所需类别的相同机制。
该模型是一个木头网站,一些人喜欢浏览默认的模板风格,但其他人更愿意看到的链接列表。
目标是创建一个模板,其中类别将通过链接传递给模板,而不必为每个类别创建一个新的列表模板。
发布于 2020-05-12 14:55:14
为什么不使用元字段呢?这样,您就可以确定要为特定类别模板使用哪个模板。
在您的循环中,只需获取要加载的当前类别:
if( have_posts() ) {
while( have_posts() ) {
the_post();
$meta_value = get_post_meta( get_the_ID() , 'meta-field', true );
if($meta_value == 'template1') {
//I prefer using this method. Create folder templates with file content-template1.php where you can design your content. That way it is way more clear.
get_template_part('templates/content','template1');
// or you can use this method
the_title();
}
}
// Very Important
wp_reset_postdata();
}https://wordpress.stackexchange.com/questions/366241
复制相似问题