Php Wordpress模板:找不到对象
我在根目录中的Php Wordpress模板:找不到对象,php,wordpress,templates,phpmyadmin,Php,Wordpress,Templates,Phpmyadmin,我在根目录中的index.PHP旁边创建了一个新的PHP模板,名为'template-insert-posts.PHP。但是,当我尝试访问以下URL时:http://localhost/wordpress/template-insert-posts.php,我得到以下错误: 找不到对象!在此服务器上找不到请求的URL。如果您手动输入URL,请检查拼写并重试。 我可以通过输入URLindex.php来访问index.php模板http://localhost/wordpress/index.php
index.PHP
旁边创建了一个新的PHP模板,名为'template-insert-posts.PHP
。但是,当我尝试访问以下URL时:http://localhost/wordpress/template-insert-posts.php
,我得到以下错误:
找不到对象!在此服务器上找不到请求的URL。如果您手动输入URL,请检查拼写并重试。
我可以通过输入URLindex.php来访问index.php
模板http://localhost/wordpress/index.php
现在,index.php
和template-insert-posts.php
都位于同一路径,即/opt/lampp/htdocs/wordpress/wp-content/themes/twentyeven
。那么,为什么我的模板insert posts.php
无法访问?在尝试访问Wordpress中的自定义模板之前,我是否缺少一些东西?此外,以下是该文件的源代码:
template-insert-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由于具有该名称的页面(在wp land中)不存在这一事实
当您尝试直接进入index.php
时,它确实起作用,因为在中,index.php
是WP在搜索显示页面的模板时查找的最后一个文件。由于此文件是每个主题中的必备文件,因此找到了它,因此没有404
有一个叫做的东西,它允许您创建友好的URL到您的站点,而不改变模板文件中的任何名称。如果您的URL直接附加到文件名,那么这是不可能的
WordPress主题手册有一篇非常简洁的文章,可以给你一些关于如何开始使用它们的提示。Nick Schäferhoff写了一篇精彩的文章,详细介绍了如何创建页面模板
简言之,取自WordPress主题Twenty14,页面模板的工作原理与此类似
<?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();