我想为我的WordPress博客创建一个自定义页面,将在其中执行我的PHP代码,同时保留整体网站CSS/主题/设计的一部分。

PHP代码将使用第三方api(因此我需要包括其他PHP文件)。

我该怎么做呢?

注:我没有特定的需要与WordPress API交互-除了包括某些其他PHP库,我需要我没有其他依赖的PHP代码,我想包括在WordPress页面。所以很明显,任何不需要学习WordPress API的解决方案都是最好的。


当前回答

试试这个:

/**
 * The template for displaying demo page
 *
 * template name: demo template
 *
 */

其他回答

创建模板页面是正确的答案。为此,只需将其添加到您在主题文件夹内创建的页面中:

<?php
    /*
    Template Name: mytemplate
    */
?>

为了运行这段代码,您需要从后端选择“mytemplate”作为页面的模板。

请查看此链接以获得正确的详细信息https://developer.wordpress.org/themes/template-files-section/page-template-files/。

你可以把你的文件命名为“newpage.php”——把它放在wp-content的主题目录中。您可以将其作为页面模板(参见http://codex.wordpress.org/Pages…),也可以将其包含在主题中的一个PHP文件中,例如header.php或single.php。

更好的是,创建一个子主题并把它放在那里,这样你就不用管你的主题代码,而且它更容易更新。

http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates

您也可以直接使用PHP页面,比如创建PHP页面并使用完整路径运行。

比如,http://localhost/path/filename.php

在WordPress中添加PHP页面到页面模板中的主题或子主题文件夹的最佳方法。

如何在WordPress中创建页面模板。

创建一个名为template-custom.php的文件,并将其放在/wp-content/theme/my-theme/目录下。

<?php
 /*
 * Template Name: Custom Template
 * Custom template used for custom php code display
 * @package   Portafolio WordPress Theme
 * @author    Gufran Hasan
 * @copyright Copyright templatecustom.com
 * @link      http://www.templatecustom.com
 */
?>
<?php get_header(); ?>
<?php
  //write code here

 ?>

<?php get_footer(); ?>

欲知详情

除了创建一个自定义模板文件并将该模板分配给一个页面(就像在已接受答案的例子中),还有一种方法是使用模板命名约定,WordPress用于加载模板(模板层次结构)。

创建一个新页面,并使用该页的slug作为模板文件名(创建一个名为page-{slug}.php的模板文件)。WordPress将自动加载符合此规则的模板。