php - 创建可在各种元素/类中的可重用 ACF 字段
问题描述
我在它自己的文件中有以下代码,它获取一个 ACF 字段并输出它。
<?=get_field('text') ?>
然后我使用 PHP include 将其包含在另一个模板文件中,这意味着我在整个站点中都有可重用的字段。这在过去运作良好,因为我可以创建一致的“文本”字段,例如:<h1>My Text field</h1>
但是,我想通过编写某种函数来进一步扩展这一点,该函数允许我调用 PHP 包含,同时还分配容器标签(h1、h2 等)以及可选类:
<h1>My Text field</h1>
<h2>My Text field</h2>
<p>My Text field</p>
<h1 class="myClass">My Text field</h1>
这可能吗?
解决方案
你可以这样做:
<?php
class functions{
public static function get_field($text = 'Default', $tag = 'p', $class = false){
if($class){
$class = ' class="' . $class . '"';
}
return '<' . $tag . $class . '>' . $text . '</' . $tag . '>';
}
}
echo functions::get_field('My Text', 'h1', 'my-class');
?>
在 HTML 中
<h1 class="my-class">My Text</h1>
希望它能让您了解如何扩展此方法。
推荐阅读
- javascript - 如何在plotly.js中的不同modeBarButtons之间切换
- sql - 是否可以通过 SUM(*) 之类的表达式聚合每个 BigQuery 列
- python - 您如何将列表中的多个数字相加并合并?(Python)
- python - python ModuleNotFoundError:没有名为“Common_Methods”的模块
- oracle - Oracle 中的合并概念
- sql - SQL:将某些记录从一个表插入到另一个表中,并使用查询添加一些其他字段
- python - 我尝试在python,tkinter中使用从一个类到另一个类的tk enrty文本
- c++ - 不同的函数签名 C++
- python - 可视化文档嵌入和聚类
- c++ - 在 QSettings 中保存矩阵