Wordpress 模板:找不到对象
Wordpress template: Object not found
我在名为 'template-insert-posts.php
的 index.php
旁边的根目录中创建了一个新的 PHP 模板。但是,当我尝试访问以下 URL: 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.
我可以通过输入 URL http://localhost/wordpress/index.php
来访问 index.php
模板
现在,index.php
和 template-insert-posts.php
都位于同一路径,即 /opt/lampp/htdocs/wordpress/wp-content/themes/twentyfifteen
。那么,为什么我的template-insert-posts.php
无法访问呢?在尝试访问 Wordpress 中的自定义模板之前,我是否遗漏了什么?另外,这里是文件的源代码:
模板插入-posts.php
<?php
$postTitleError = '';
if ( isset( $_POST['submitted'] ) && isset( $_POST['post_nonce_field'] ) && wp_verify_nonce( $_POST['post_nonce_field'], 'post_nonce' ) ) {
if ( trim( $_POST['postTitle'] ) === '' ) {
$postTitleError = 'Please enter a title.';
$hasError = true;
}
$post_information = array(
'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
'post_content' => $_POST['postContent'],
'post_type' => 'post',
'post_status' => 'pending'
);
wp_insert_post( $post_information );
}
$post_id = wp_insert_post( $post_information );
if ( $post_id ) {
wp_redirect( home_url() );
exit;
}
?>
<?php
get_header(); ?>
<form action="" id="primaryPostForm" method="POST">
<fieldset>
<label for="postTitle"><?php _e('Post Title:', 'framework') ?></label>
<input type="text" name="postTitle" id="postTitle" class="required" value="<?php if ( isset( $_POST['postTitle'] ) ) echo $_POST['postTitle']; ?>"/>
</fieldset>
<fieldset>
<label for="postContent"><?php _e('Post Content:', 'framework') ?></label>
<textarea name="postContent" id="postContent" rows="8" cols="30" class="required" <?php if ( isset( $_POST['postContent'] ) ) { if ( function_exists( 'stripslashes' ) ) { echo stripslashes( $_POST['postContent'] ); } else { echo $_POST['postContent']; } } ?>></textarea>
</fieldset>
<fieldset>
<input type="hidden" name="submitted" id="submitted" value="true" />
<?php wp_nonce_field( 'post_nonce', 'post_nonce_field' ); ?>
<button type="submit"><?php _e('Add Post', 'framework') ?></button>
</fieldset>
</form>
<?php get_footer(); ?>
发生这种情况是因为模板在 WordPress 中不是这样工作的。您无需为网站中的每个页面创建特定文件。您创建页面,然后为它们分配模板,让 WordPress 确定如何访问和创建对这些页面的访问。尝试直接访问其中一个文件将产生 404,因为 WordPress 不存在具有该名称的 page(在 wp 领域)。
当您尝试直接进入 index.php
时,它 确实 起作用的事实是,在 template hierarchy 中, index.php
是WP 在搜索用于显示页面的模板时查找的最后一个文件。由于这个文件是每个主题的必备文件,所以找到了,因此没有404s。
有一种叫做 permalinks 的东西,它允许您在不更改模板文件中的任何名称的情况下为您的站点创建友好的 URL。如果您的 URL 直接附加到文件名,那将是不可能的。
WordPress 主题手册在 page templates, and the codex can give you some hints on how to get started with them. Smashing Magazine 上有一篇非常简洁的文章,由 Nick Schä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
,如果没有找到其他模板文件。
我在名为 'template-insert-posts.php
的 index.php
旁边的根目录中创建了一个新的 PHP 模板。但是,当我尝试访问以下 URL: 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.
我可以通过输入 URL http://localhost/wordpress/index.php
index.php
模板
现在,index.php
和 template-insert-posts.php
都位于同一路径,即 /opt/lampp/htdocs/wordpress/wp-content/themes/twentyfifteen
。那么,为什么我的template-insert-posts.php
无法访问呢?在尝试访问 Wordpress 中的自定义模板之前,我是否遗漏了什么?另外,这里是文件的源代码:
模板插入-posts.php
<?php
$postTitleError = '';
if ( isset( $_POST['submitted'] ) && isset( $_POST['post_nonce_field'] ) && wp_verify_nonce( $_POST['post_nonce_field'], 'post_nonce' ) ) {
if ( trim( $_POST['postTitle'] ) === '' ) {
$postTitleError = 'Please enter a title.';
$hasError = true;
}
$post_information = array(
'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
'post_content' => $_POST['postContent'],
'post_type' => 'post',
'post_status' => 'pending'
);
wp_insert_post( $post_information );
}
$post_id = wp_insert_post( $post_information );
if ( $post_id ) {
wp_redirect( home_url() );
exit;
}
?>
<?php
get_header(); ?>
<form action="" id="primaryPostForm" method="POST">
<fieldset>
<label for="postTitle"><?php _e('Post Title:', 'framework') ?></label>
<input type="text" name="postTitle" id="postTitle" class="required" value="<?php if ( isset( $_POST['postTitle'] ) ) echo $_POST['postTitle']; ?>"/>
</fieldset>
<fieldset>
<label for="postContent"><?php _e('Post Content:', 'framework') ?></label>
<textarea name="postContent" id="postContent" rows="8" cols="30" class="required" <?php if ( isset( $_POST['postContent'] ) ) { if ( function_exists( 'stripslashes' ) ) { echo stripslashes( $_POST['postContent'] ); } else { echo $_POST['postContent']; } } ?>></textarea>
</fieldset>
<fieldset>
<input type="hidden" name="submitted" id="submitted" value="true" />
<?php wp_nonce_field( 'post_nonce', 'post_nonce_field' ); ?>
<button type="submit"><?php _e('Add Post', 'framework') ?></button>
</fieldset>
</form>
<?php get_footer(); ?>
发生这种情况是因为模板在 WordPress 中不是这样工作的。您无需为网站中的每个页面创建特定文件。您创建页面,然后为它们分配模板,让 WordPress 确定如何访问和创建对这些页面的访问。尝试直接访问其中一个文件将产生 404,因为 WordPress 不存在具有该名称的 page(在 wp 领域)。
当您尝试直接进入 index.php
时,它 确实 起作用的事实是,在 template hierarchy 中, index.php
是WP 在搜索用于显示页面的模板时查找的最后一个文件。由于这个文件是每个主题的必备文件,所以找到了,因此没有404s。
有一种叫做 permalinks 的东西,它允许您在不更改模板文件中的任何名称的情况下为您的站点创建友好的 URL。如果您的 URL 直接附加到文件名,那将是不可能的。
WordPress 主题手册在 page templates, and the codex can give you some hints on how to get started with them. Smashing Magazine 上有一篇非常简洁的文章,由 Nick Schä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
,如果没有找到其他模板文件。