我在我的根目录中创建了一个新的PHP模板,旁边是名为index.php
的{{1}}。但是,当我尝试访问以下网址时:'template-insert-posts.php
,我收到以下错误:
http://localhost/wordpress/template-insert-posts.php
我可以通过输入网址Object not found! The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.
index.php
模板
现在,http://localhost/wordpress/index.php
和index.php
都位于相同的路径中,即template-insert-posts.php
。那么,为什么我的/opt/lampp/htdocs/wordpress/wp-content/themes/twentyfifteen
无法访问?在尝试访问Wordpress中的自定义模板之前,我有什么遗漏的东西吗?此外,这里是文件的源代码:
模板插入件posts.php
template-insert-posts.php
答案 0 :(得分:1)
发生这种情况是因为这不是WordPress中模板的工作方式。您不为网站中的每个页面创建特定文件。您创建页面,然后为它们分配模板,让WordPress找出如何访问和创建对这些页面的访问。尝试直接访问其中一个文件将产生404,因为WordPress由于具有该名称的页(在wp land中)不存在。
当您尝试直接进入index.php
时, 工作的事实是因为,在template hierarchy中,index.php
是WP查找的最后一个文件搜索从中显示页面的模板时。由于这个文件是每个主题必备的,所以它被发现,因此没有404s。
有一些名为permalinks的内容,可让您为网站创建友好的网址,而无需更改模板文件中的任何名称。如果您的网址直接附加到文件名,那将是不可能的。
WordPress主题手册在page templates上有一篇非常简洁的文章,codex可以为你提供一些关于如何开始使用它们的提示。 Smashing Magazine有一篇由NickSchäferhoff撰写的精彩文章,其中详细说明了如何创建页面模板。
简而言之,取自WordPress主题Twentyfourteen,页面模板的工作方式有点像这样
<?php
/**
* Template Name: Full Width Page
*
* @package WordPress
* @subpackage Twenty_Fourteen
* @since Twenty Fourteen 1.0
*/
get_header(); ?>
<div id="main-content" class="main-content">
<?php
if ( is_front_page() && twentyfourteen_has_featured_posts() ) {
// Include the featured content template.
get_template_part( 'featured-content' );
}
?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php
// Start the Loop.
while ( have_posts() ) : the_post();
// Include the page content template.
get_template_part( 'content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
endwhile;
?>
</div><!-- #content -->
</div><!-- #primary -->
</div><!-- #main-content -->
<?php
get_sidebar();
get_footer();
有趣的是,评论部分Template Name: Full Width Page
使这个模板成为全局模式,这意味着它可以在您的网站内的任何位置访问(有关层次结构的详细信息,请查看文档)。在模板上有类似内容之后,创建一个页面,然后为其分配模板。你应该是金色的!
修改强>
仍然及时检查此awesome infographic,其中显示了如何在WP版本中运行模板,以及如果找不到其他模板文件,每个页面最终如何呈现给index.php
。