例如:
site.com/contact
(或site.com/?page=contact
如果不使用永久链接)
当有人转到那个页面时,我想加载一个主题模板,比如page-contact.php
但我不想在仪表板中为这项工作创建页面。我如何在内部、函数内做到这一点。php?
我知道这是可能的,因为bbpress插件以某种方式为/用户/页面实现了这一点,而这些页面不是标准的wordpress页面。。。
例如:
site.com/contact
(或site.com/?page=contact
如果不使用永久链接)
当有人转到那个页面时,我想加载一个主题模板,比如page-contact.php
但我不想在仪表板中为这项工作创建页面。我如何在内部、函数内做到这一点。php?
我知道这是可能的,因为bbpress插件以某种方式为/用户/页面实现了这一点,而这些页面不是标准的wordpress页面。。。
这就是我现在在一个网站上使用的技术。它涉及为“模板”注册一个查询变量,并注册一个名为/contact
, 内部重写为?template=contact
. 然后,只需在template\\u重定向挂钩处检查该查询变量,如果存在,则包括要加载和退出的页面模板。
/* Add a query var for template select, and and endpoint that sets that query var */
add_action( \'init\', \'wpse22543_rewrite_system\' );
function wpse22543_rewrite_system() {
global $wp, $wp_rewrite;
$wp->add_query_var( \'template\' );
add_rewrite_endpoint( \'contact\', EP_ROOT );
$wp_rewrite->add_rule( \'^/contact/?$\',
\'index.php?template=contact\', \'bottom\' );
$wp_rewrite->flush_rules();
}
/* Handle template redirect according the template being queried. */
add_action( \'template_redirect\', \'wpse22543_select_template\' );
function wpse22543_select_template() {
global $wp;
$template = $wp->query_vars;
if ( array_key_exists( \'template\', $template ) &&
\'contact\' == $template[\'template\'] ) {
/* Make sure to set the 404 flag to false, and redirect
to the contact page template. */
global $wp_query;
$wp_query->set( \'is_404\', false );
include( get_stylesheet_directory().\'/contact.php\' );
exit;
}
}
这将服务于您的联系人。php文件是否有任何请求?模板=联系人或/联系人。也许有一种更快的方法,但这是有效的。这里有一个更全面的教程:Create Fake Pages in WordPress.
你可以照你说的做。自2.9版起,它就被内置到Wordpress的模板层次结构中。
如果您有一个名为Contact的页面,请创建一个名为page Contact的页面模板。与页面不同的php。php。瞧,它起作用了。
来自WP文档。
按页面ID或页面段塞列出的模板
您可以使用不同的归档文件“页面”,名称中带有ID或slug。仅适用于2.9版。
示例:
page-{id}.php
page-{slug}.php
这是你想要的吗?在使用多个页面模板的主题上处理CSS的最佳实践是什么?例如:我将有一个全宽页面模板和一个两列页面模板。最好只在单个CSS中调用容器(#fullWidthContainer vs.#twoColumnContainer)还是为每个页面模板提供单独的CSS文件更好?