使用 php 代码获取 page.php
Get page.php using php code
我正在使用 wordpress。
我看到在我的 index.php 中有一个代码叫做 <?php get_footer(); ?>
..我明白了,这很简单。此代码将拉 footer.php
.
在我看来,get_footer()
内置于 wordpress 中,可以提取任何名为 footer.php
.
的内容
我创建了自己的页面 page.php
。
我希望能够 'get' 此页面并在我的 php 代码中显示 'sidebar widget'。
我试过粘贴这段代码,但我更确定它是错误的:
<?php
echo file_get_contents("side.php");
?>
如果我想要显示名为 page.php
的自定义页面,我需要什么代码?
尝试以下方法
include ('side.php');
或
require ('side.php');
您也可以使用 include_once / require_once
page.php是wordpress的一个特殊页面。看这张图。
https://developer.wordpress.org/files/2014/10/template-hierarchy.png.
wordpress的方式是自己创建模板。
https://developer.wordpress.org/themes/template-files-section/page-template-files/page-templates/
在您的 php 页面中添加此代码
<?php include_once('filename.php'); ?>
请使用 require_once 或包含在 php 文件中,如下所示。
require_once('side.php');
或
include ('side.php');
试试这个,
require( dirname( __FILE__ ) . '/page.php' );
如果您的页面与您的父页面处于同一级别。
WordPress加载模板文件的方式是get_template_part()
:
get_template_part( 'page' );
// loads page.php from the theme directory.
请注意,page.php
是WordPress主题中的一个特殊文件名,如果显示页面,该文件将作为模板加载。如果你想避免这种情况,你应该给你的文件一个不同的名字。
详情在前面已经提到了template-hierarchy.png
编辑:
重读你的问题后:如果你的 page.php
不是来自模板,而是在你作为侧边栏小部件开发的插件的文件夹中,也已经提到的 include('page.php');
将是干得好。
有很多选项供您选择:
您可以使用 get_template_part()
:
From the Docs: WordPress now offers a function, get_template_part()
,
that is part of the native API and is used specifically for reusing
sections - or templates - of code (except for the header, footer, and
sidebar) through your theme.
其他解决方案是 require_once()
或 include_once()
。
但是如果我比较两个函数 include_once()
比 require_once()
对于较小的应用程序更快。
为了更好地了解 require
和 include
,您可以阅读此 Question。
我正在使用 wordpress。
我看到在我的 index.php 中有一个代码叫做 <?php get_footer(); ?>
..我明白了,这很简单。此代码将拉 footer.php
.
在我看来,get_footer()
内置于 wordpress 中,可以提取任何名为 footer.php
.
我创建了自己的页面 page.php
。
我希望能够 'get' 此页面并在我的 php 代码中显示 'sidebar widget'。
我试过粘贴这段代码,但我更确定它是错误的:
<?php
echo file_get_contents("side.php");
?>
如果我想要显示名为 page.php
的自定义页面,我需要什么代码?
尝试以下方法
include ('side.php');
或
require ('side.php');
您也可以使用 include_once / require_once
page.php是wordpress的一个特殊页面。看这张图。 https://developer.wordpress.org/files/2014/10/template-hierarchy.png.
wordpress的方式是自己创建模板。 https://developer.wordpress.org/themes/template-files-section/page-template-files/page-templates/
在您的 php 页面中添加此代码
<?php include_once('filename.php'); ?>
请使用 require_once 或包含在 php 文件中,如下所示。
require_once('side.php');
或
include ('side.php');
试试这个,
require( dirname( __FILE__ ) . '/page.php' );
如果您的页面与您的父页面处于同一级别。
WordPress加载模板文件的方式是get_template_part()
:
get_template_part( 'page' );
// loads page.php from the theme directory.
请注意,page.php
是WordPress主题中的一个特殊文件名,如果显示页面,该文件将作为模板加载。如果你想避免这种情况,你应该给你的文件一个不同的名字。
详情在前面已经提到了template-hierarchy.png
编辑:
重读你的问题后:如果你的 page.php
不是来自模板,而是在你作为侧边栏小部件开发的插件的文件夹中,也已经提到的 include('page.php');
将是干得好。
有很多选项供您选择:
您可以使用 get_template_part()
:
From the Docs: WordPress now offers a function,
get_template_part()
, that is part of the native API and is used specifically for reusing sections - or templates - of code (except for the header, footer, and sidebar) through your theme.
其他解决方案是 require_once()
或 include_once()
。
但是如果我比较两个函数 include_once()
比 require_once()
对于较小的应用程序更快。
为了更好地了解 require
和 include
,您可以阅读此 Question。