【问题标题】:Wordpress child theme, creating a custom php template pageWordpress 子主题,创建自定义 php 模板页面
【发布时间】:2023-03-03 18:38:01
【问题描述】:

我目前正在开发一个使用子主题(店面是父主题)的项目。我需要能够使用纯 php / javascript(wordpress 管理员之外)进行编码。其他自定义模板页面有*-content.php结构,如:foo-content.php

我创建了我想要开发的自定义页面。但是,当访问 url 时,我得到一个空白页。我很难理解这是为什么。

此外,我尝试进入 wordpress 管理员并在管理员中创建页面,使其与我创建的文件名 my.domain.com/foo 相匹配,但似乎没有任何效果 - 我一直得到一个空白页面并且没有错误。任何想法为什么会这样?

感谢任何建议。

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案1】:

    如果您特别需要制作页面模板,请阅读主题开发者手册的Page Template Files 部分。

    关键点是您需要在文件中包含一个页面模板标题,其中至少包含一个模板名称,如下所示:

    <?php
    /*
        Template Name: Foo Content
    */
    // Your code here
    

    然后您将进入 wp-admin 并创建一个新页面,然后在右侧的模板选择器中选择您的 Foo 内容页面模板。

    如果不是您需要的“页面模板”,您也可以查看Template Hierarchy

    【讨论】:

    • 感谢您的回复。我会试试这个。然而,奇怪的是我试图遵循与其他自定义模板页面相同的流程 - 其他页面没有模板名称注释。知道为什么会这样吗?
    • 不幸的是,这将归结为实际主题。它们可能不是实际的“页面模板”,可能是主题中内置的某种自定义模板引擎。您需要解析主题并准确了解not_foo-content.php 的加载方式、时间和原因,可能是在template_redirect 钩子或template_include 过滤器上?
    【解决方案2】:

    您是否包含了 index.php 文件?没有它,您的网站将没有起点,您不会看到任何错误,而只会看到一个空白页面。我会确保你有一个 index.php 在那里。它应该包含:

    <?php
    /**
     * Front to the WordPress application. This file doesn't do anything, but loads
     * wp-blog-header.php which does and tells WordPress to load the theme.
     *
     * @package WordPress
     */
    
    /**
     * Tells WordPress to load the WordPress theme and output it.
     *
     * @var bool
     */
    define('WP_USE_THEMES', true);
    
    /** Loads the WordPress Environment and Template */
    require( dirname( __FILE__ ) . '/wp-blog-header.php' );
    

    【讨论】:

      【解决方案3】:

      如果您创建一个新的 WP 模板(作为一个 php 文件),您之后必须在 Wordpress 后端创建一个页面并选择该模板文件作为您新创建的 WP 页面的模板。然后该页面的 url 将成为该 WP 页面的 slug。保存后从 WP 编辑器中打开文件,然后您会看到其 URL。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多