【问题标题】:Wordpress template: Object not foundWordpress 模板:找不到对象
【发布时间】:2015-11-04 08:33:13
【问题描述】:

我在我的根目录中创建了一个新的 PHP 模板,旁边是 index.php,名为 'template-insert-posts.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.phptemplate-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(); ?>

【问题讨论】:

    标签: php wordpress templates phpmyadmin


    【解决方案1】:

    发生这种情况是因为模板在 WordPress 中不是这样工作的。您无需为网站中的每个页面创建特定文件。您创建页面,然后为它们分配模板,让 WordPress 弄清楚如何访问和创建对这些页面的访问。尝试直接访问其中一个文件将产生 404,因为 WordPress 由于不存在具有该名称的 page (在 wp 土地中)这一事实。

    当您尝试直接进入index.php 时它确实 起作用的事实是,在template hierarchy 中,index.php 是 WP 在搜索模板时查找的最后一个文件显示您的页面。由于这个文件是每个主题的必备文件,所以找到了,因此没有 404。

    有一个叫做permalinks 的东西,它允许您在不更改模板文件中的任何名称的情况下为您的网站创建友好的 URL。如果您的 URL 直接附加到文件名,那将是不可能的。

    WordPress 主题手册在page templates 上有一篇非常简洁的文章,codex 可以为您提供一些关于如何开始使用它们的提示。 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

    【讨论】:

      猜你喜欢
      • 2017-07-25
      • 1970-01-01
      • 2018-11-01
      • 1970-01-01
      • 2017-04-27
      • 1970-01-01
      • 1970-01-01
      • 2020-05-03
      • 1970-01-01
      相关资源
      最近更新 更多