【问题标题】:Adding WordPress header and footer to a PHP script将 WordPress 页眉和页脚添加到 PHP 脚本
【发布时间】:2016-10-05 14:43:04
【问题描述】:

我正在尝试在 WordPress 中创建自定义表单。表单的第 1 步是 HTML 代码,它收集数据并通过 post 方法将其发送到 PHP 文件,然后将其写入 MySQL 数据库并使用 PHP 代码创建表单的第 2 步。我的问题是我想在步骤 1 中 WordPress 使用的表单的步骤 2 中包含默认的 WordPress 页眉和页脚。有没有办法通过在我的 PHP 脚本中包含 header.php 和 footer.php 的代码来做到这一点?

【问题讨论】:

    标签: php mysql wordpress get-headers


    【解决方案1】:

    你是说这个吗?

    <?php get_header(); ?>
    

    对于页脚:

    <?php get_footer(); ?>
    

    如果你看看get_header() function

     function get_header( $name = null ) {
            /**
             * Fires before the header template file is loaded.
             *
             * The hook allows a specific header template file to be used in place of the
             * default header template file. If your file is called header-new.php,
             * you would specify the filename in the hook as get_header( 'new' ).
             *
             * @since 2.1.0
             * @since 2.8.0 $name parameter added.
             *
             * @param string $name Name of the specific header file to use.
             */
            do_action( 'get_header', $name );
    
            $templates = array();
            $name = (string) $name;
            if ( '' !== $name ) {
                    $templates[] = "header-{$name}.php";
            }
    
            $templates[] = 'header.php';
    
            locate_template( $templates, true );
    }
    

    你的根主题是否至少有一个 header.php 文件?

    常见的 Wordpress 结构:

    your_project_folder
        -wp-admin
        -wp-content
            -languages
            -plugins
            -themes
                -YOUR_THEME_FOLDER
                    -[HERE YOU PLACE YOUR index.php file and header.php for example, and inside index.php you place your get_header() function]
            -upgrade
            -uploads
        -wp-includes
        index.php
        wp-activate.php
        wp-blog-header.php
        wp-comments-post.php
        wp-config.php
        wp-cron.php
        [...more files]
    

    【讨论】:

    • 我正在尝试使用以下代码:谢谢你的表格。

      你说你是$age 岁,$supplement_user 使用补充剂。

      "; ?>
    • 这是我得到的错误:致命错误:未捕获的错误:调用 /home/whatsu13/public_html/whatsuppnews.com/wp-content/themes/Avada/handle_form 中未定义的函数 get_header()。 php:2 堆栈跟踪:#0 {main} 在第 2 行 /home/whatsu13/public_html/whatsuppnews.com/wp-content/themes/Avada/handle_form.php 中抛出
    • 为什么会出现“未定义函数 get_header()”错误?
    • 当您引用 get_header() 之类的函数时,是否需要在特定目录中才能让 Wordpress 识别函数调用?
    猜你喜欢
    • 2021-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-23
    • 2010-12-03
    • 1970-01-01
    • 2018-08-17
    相关资源
    最近更新 更多