首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否有可能在链接地址包含特定类别的多个类别上使用模板?

问是否有可能在链接地址包含特定类别的多个类别上使用模板?
EN

WordPress Development用户
提问于 2020-05-09 16:05:25
回答 1查看 20关注 0票数 0

这个问题是为了对WordPress模板文件的使用进行非常具体的细化。细化的工作方式类似于默认的archive.php和变体的工作方式,但我们希望有第二个模板,它具有通过链接地址查询所需类别的相同机制。

该模型是一个木头网站,一些人喜欢浏览默认的模板风格,但其他人更愿意看到的链接列表。

目标是创建一个模板,其中类别将通过链接传递给模板,而不必为每个类别创建一个新的列表模板。

EN

回答 1

WordPress Development用户

回答已采纳

发布于 2020-05-12 14:55:14

为什么不使用元字段呢?这样,您就可以确定要为特定类别模板使用哪个模板。

在您的循环中,只需获取要加载的当前类别:

代码语言:javascript
复制
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();
}
票数 1
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/366241

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档