首页 文章

在自定义php页面中包含wordpress主题

提问于
浏览
4

我需要在Wordpress中包含一个自定义PHP页面 .

所以我需要做的就是使用Wordpress上安装的Wordpress主题来显示这个自定义的php页面 .

不介意哪个主题,自定义php页面必须在任何主题下显示在那一刻 .

我怎么在Wordpress中做到这一点?

我是Wordpress开发的新手 .

谢谢

4 回答

  • 1

    创建一个能够在任何主题中查看(并应用主题)的自定义php页面将非常困难 .

    每个wordpress页面调用该特定主题的特定主题功能,以及引用该主题的文件以生成页眉,页脚,css文件,javascript文件等 . 您的自定义页面需要计划所有这些突发事件,每个可能使用的主题 .

    这是另一种解决方案:通过此插件将PHP代码 directly 注入标准的wordpress页面http://wordpress.org/extend/plugins/allow-php-in-posts-and-pages/

    含义:你创建一个普通的wordpress页面,但能够添加php . 呈现此页面时,将使用正确的页面模板,并为您处理所有主题参考 .

  • 0

    您可以使用页面模板轻松完成此操作 . WordPress允许您创建页面模板,可以通过页面编辑器中的'Page Attributes'面板将其分配给页面 . 这些模板是主题目录中的php文件,以一些代码开头(有关详细信息,请参阅this page in The Codex):

    <?php 
    /*
    Template name: Custom PHP Page
    */
    ?>
    
    <?php // begin custom PHP page ?>
    

    通常,模板是常规主题文件(例如page.php)的变体,并且将调用get_header()和get_footer()函数并具有循环的实例 . 但是,如果您只想使用自定义PHP页面,那么您需要做的就是在当前主题目录中创建所需的文件,并在文件的最顶部添加上述代码 .

    要在您的站点上输出自定义PHP页面,您需要通过管理区域添加新页面,然后将新页面模板分配给此页面 .

    或者,如果要在现有主题文件中包含自定义PHP页面,请使用以下代码:

    <?php include(TEMPLATEPATH . '/includes/file.php'); ?>
    

    在这种情况下,您的自定义PHP文件将位于当前主题目录中名为“includes”的目录中 .

    蒂姆 .

  • 4

    这并不困难 . 这就是你需要的:

    一旦你包含了主要的wordpress博客 Headers ,你就可以使用整个wordpress功能的设备,它允许你获得活动主题的目录 . 一旦你得到它,只需包括主题的页眉和页脚 .

    // If title is not displayed before loading the header, Wordpress displays "Page not found" as the title
    
    echo "<head>
    <title>Your page title</title>
    </head>";
    
    // Include the Main Wordpress blog header
    include $_SERVER['DOCUMENT_ROOT']."/wp-blog-header.php";
    
    //Now, you need to get the active theme's folder, and get a relative path to that folder
    
    $homeurl=home_url();
    $ddir= get_bloginfo( 'template_directory');
    $current_theme_relative_path=substr_replace($ddir, "", 0, strlen($homeurl));
    //echo "
    The relative path to the currently active theme is ".$current_theme_relative_path; //Once you have the path, include the header and footer, adding your custom php code in between. // Include the specific theme header you need include $_SERVER['DOCUMENT_ROOT'].$current_theme_relative_path."/header.php"; // Your custom PHP code STARTS here // Add anything you want to display to the user echo " <h2> Your form has been submitted </h2>"; // END of custom code ?> <?php } // Now end with the theme's footer include $_SERVER['DOCUMENT_ROOT'].$current_theme_relative_path."/footer.php"; ?>
  • 2

    非常有帮助(即使是2011-13的日期)

    另外,谢谢你,我正在分享我制作的版本

    如果你的wordpress文件夹不在ROOT,这很有用

    PasBin Link - wordpress custom php page

    只需更改$ wplocalpath的值:

    // Wordpress路径(如果wordpress不在ROOT

    // $ wplocalpath =“/ Wordpress1”;

相关问题