我想为我的WordPress博客创建一个自定义页面,将在其中执行我的PHP代码,同时保留整体网站CSS/主题/设计的一部分。
PHP代码将使用第三方api(因此我需要包括其他PHP文件)。
我该怎么做呢?
注:我没有特定的需要与WordPress API交互-除了包括某些其他PHP库,我需要我没有其他依赖的PHP代码,我想包括在WordPress页面。所以很明显,任何不需要学习WordPress API的解决方案都是最好的。
我想为我的WordPress博客创建一个自定义页面,将在其中执行我的PHP代码,同时保留整体网站CSS/主题/设计的一部分。
PHP代码将使用第三方api(因此我需要包括其他PHP文件)。
我该怎么做呢?
注:我没有特定的需要与WordPress API交互-除了包括某些其他PHP库,我需要我没有其他依赖的PHP代码,我想包括在WordPress页面。所以很明显,任何不需要学习WordPress API的解决方案都是最好的。
当前回答
您也可以直接使用PHP页面,比如创建PHP页面并使用完整路径运行。
比如,http://localhost/path/filename.php
其他回答
<?php /* Template Name: CustomPageT1 */ ?>
<?php get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
// Start the loop.
while ( have_posts() ) : the_post();
// Include the page content template.
get_template_part( 'template-parts/content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
// End of the loop.
endwhile;
?>
</main><!-- .site-main -->
<?php get_sidebar( 'content-bottom' ); ?>
</div><!-- .content-area -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
在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(); ?>
欲知详情
创建一个名为my-page.php的页面,并将其保存在主题目录下。 现在,编辑这个php文件,并在页面顶部写入以下一行
<?php /* Template Name: My Page */ ?>
在自定义页面定义行下编写PHP代码,您可以调用您的其他WP模板,该文件中的函数。
开始喜欢 <?php require_once (header。php); ?>或
无论你用什么方式来整合页眉和页脚以保持布局的一致性。
因为这是一个我的页面,你需要从WordPress管理面板创建一个页面。 转到Admin => Pages =>添加新的
添加一个页面标题,这取决于您如何编写自定义页面,您也可以添加页面主体(描述)。如果是在自定义php页面中编写的,则可以完全跳过描述。
在右侧,选择Template。 从下拉菜单中选择我的自定义页面。 你都准备好了!转到[wordpress][1]创建的slug(永久链接)并查看页面。
创建模板页面是正确的答案。为此,只需将其添加到您在主题文件夹内创建的页面中:
<?php
/*
Template Name: mytemplate
*/
?>
为了运行这段代码,您需要从后端选择“mytemplate”作为页面的模板。
请查看此链接以获得正确的详细信息https://developer.wordpress.org/themes/template-files-section/page-template-files/。
你不需要与API交互或使用插件。
首先,在主题文件夹(在/wp-content/themes/themename/下)中复制post.php或page.php。
将新文件重命名为templatename.php(其中templatename是对新模板的称呼)。要将新模板添加到可用模板列表中,请在新文件的顶部输入以下内容:
<?php
/*
Template Name: Name of Template
*/
?>
您可以修改这个文件(使用PHP)以包含其他文件或任何您需要的文件。
然后在WordPress博客中创建一个新页面,在页面编辑屏幕中,您将在右边的Attributes小部件中看到一个Template下拉菜单。选择新模板并发布页面。
新页面将使用templatename.php中定义的PHP代码
来源:创建供全局使用的自定义页面模板