WordPress的模板:找不到对象 [英] Wordpress template: Object not found

查看:98
本文介绍了WordPress的模板:找不到对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在根目录内的index.php旁边创建了一个名为'template-insert-posts.php的新PHP模板.但是,当我尝试访问以下URL:http://localhost/wordpress/template-insert-posts.php时,出现以下错误:

I created a new PHP template inside my root directory alongside index.php named 'template-insert-posts.php. However, when I try to access the following URL: http://localhost/wordpress/template-insert-posts.php, I get the following error:

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

I am able to access the index.php template by entering the URL http://localhost/wordpress/index.php

现在,index.phptemplate-insert-posts.php都位于同一路径中,即/opt/lampp/htdocs/wordpress/wp-content/themes/twentyfifteen.那么,为什么我的template-insert-posts.php无法访问?在尝试访问Wordpress中的自定义模板之前,我还缺少什么吗?另外,这是文件的源代码:

Now, both index.php and template-insert-posts.php are located in the same path ie /opt/lampp/htdocs/wordpress/wp-content/themes/twentyfifteen. Then, why is my template-insert-posts.php not accessible? Is there something I'm missing before trying to access a custom template in Wordpress? Also, here's the source code of the file:

template-insert-posts.php

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由于存在不存在具有该名称的 page (在wp区域中)的事实.

That happened because that's not how templates work in WordPress. You don't create a specific file for each page in your website. You create pages, and then you assign templates to them, and let WordPress figure out how to access and create accesses to those pages. Trying to direct access one of those files will yield a 404 because WordPress due to the fact that a page (in wp land) with that name does not exist.

当您尝试直接进入index.php did 起作用的事实是因为

The fact that it did work when you tried going directly into index.php is because , in the template hierarchy, index.php is the last file WP looks for when searching for a template from which to display your page. As this file is a must-have in every theme, it was found, and thus no 404s.

有一种叫做永久链接的东西,它使您可以创建网站的友好URL,而无需更改网站中的任何名称.模板文件.如果您的URL直接附加到文件名上,那将是不可能的.

There's something called permalinks which allows you to create friendly URLs to your site without changing any names in your template files. That would be impossible if your URLs were directly attached to the file names.

WordPress主题手册在 codex 可以为您提供一些入门指南他们. Smashing Magazine 上有一篇很棒的文章,由尼克·谢弗霍夫(NickSchäferhoff)撰写,其中提供了有关如何创建页面模板的详细说明.

WordPress Theme Handbook has a pretty neat article on page templates, and the codex can give you some hints on how to get started with them. Smashing Magazine has an amazing article, written by Nick Schäferhoff, which gives detailed instructions on how to create a page template.

简而言之,取自WordPress主题二十一十四,页面模板的工作原理如下:

In a nutshell, and taken from WordPress theme Twentyfourteen, a page template works somewhat like this

<?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使此模板成为全局模板,这意味着可以在您网站的任何位置访问该模板(请参阅文档以获取有关层次结构的更多详细信息).一旦模板上有类似的内容,请创建一个页面,然后将模板分配给它.你应该是金色的!

Interestingly enough, the comment part Template Name: Full Width Page makes this template global, which means it can be accessed anywhere within your site (take a look at the docs for more detail on hierarchy). Once you have something like that on your template, create a page, and then assign you template to it. You should be golden!

请及时检查此令人敬畏的信息图,其中显示了模板在WP land中的工作方式以及每个页面最终呈现的方式如果找不到其他模板文件,则转到index.php.

Still in time, check this awesome infographic that shows how templating works in WP land, and how every page eventually renders to index.php, if no other template file is found.

这篇关于WordPress的模板:找不到对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆