admin管理员组文章数量:1300029
Let's say i have a custom post type names "Projects", is it possible to have two template like :
- Minimal template (with a few info about the projet)
- Complete template (all the info)
And the template is selected according the url :
site/minimal-template/project-1 or site/complete-template/project-1
Is it possible ? Thanks
Let's say i have a custom post type names "Projects", is it possible to have two template like :
- Minimal template (with a few info about the projet)
- Complete template (all the info)
And the template is selected according the url :
site/minimal-template/project-1 or site/complete-template/project-1
Is it possible ? Thanks
Share Improve this question asked Mar 23, 2021 at 9:48 LinkrefLinkref 111 bronze badge1 Answer
Reset to default 0Use the query_vars
filter to add a new query variable to store the template version (minimal / complete).
add_filter( 'query_vars', 'se385568_query_vars' );
function se385568_query_vars( $vars )
{
$vars[] = 'tpl';
return $vars;
}
Add a rewrite rule (codex) to recognize the new link format and set the newly created query variable
add_action( 'init', 'se385568_rewrite_rules' );
function se385568_rewrite_rules()
{
$cpt_slug = 'projects';
add_rewrite_rule(
"^(minimal|complete)-template/([^/]+)(?:/([0-9]+))?/?$",
'index.php?'. $cpt_slug .'=$matches[2]&tpl=$matches[1]&page=$matches[3]',
'top'
);
}
Use the template_include
filter to change the loaded template based on your query variable.
add_filter( 'template_include', 'se385568_project_template', 90 );
function se385568_project_template()
{
$qv = get_query_var('tpl', null);
if ( empty($qv) )
return $template;
//
// check $qv and return path to the appropriate file
//
$template = dirname(__FILE__) . '/se385568_minimal_project.php';
return $template;
}
Remember to refresh the rewrite rules (Dashboard -> Settings -> Permalinks -> Save) after adding the code.
本文标签: customization2 Templates 1 custom post type according url
版权声明:本文标题:customization - 2 Templates 1 custom post type according url 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741656291a2390780.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论