我知道有办法做到这一点,但是,我很难理解它。这是我的问题。
我有一个短代码,它触发了一个引入商店库存的功能。我对用 HTML 返回的数据进行格式化。我的插件已经使用以下短代码执行此操作['inventory']
如果可能的话,我想做的是在同一个函数中我想创建更多的短代码,例如[product_id]
希望在我循环记录时,从同一个函数中将当前记录 product_id 作为该短代码值。
并且还将一些 WordPress 主题元素与简码结合使用。
因此,假设库存短代码返回以下内容
<div>
<h1>Product ID {$product_id}</h1>
<p>Price $price</p>
</div>
并循环遍历每个产品,因此如果有 4 个产品,它将输出上述 HTML 4 次。
我使用的主题允许我创建特定于我的主题的按钮,我不想将这些按钮硬编码到我的代码中。
我想做的是以下
[inventory]
['record']
//Insert theme buttons using themes builder
<button value=['product_id']>Get more info</button>
['/record]
[/inventory]
所以我想做的是拥有库存,生成要输出的数据,而不是循环并输出 id,而是循环并将数据传递给 ['record'] 短代码,然后让该标签呈现使用每条记录下方的按钮进行输出。并为按钮值提供 product_id 短代码,它将保存当前记录的产品 ID。
我想说 do_shortcode 涉及,但我不太确定如何实现这一点。
任何帮助表示赞赏
我试过阅读文档。
function inventory($atts, $content = null){
extract(shortcode_atts(array(
'storeid' => 'default',
), $atts));
//query that returns the store inventory
$query;
//Output formatted results FYI there is a whole function that but it pretty much just loops through the $query results.
foreach($query as $queryResult){
echo $queryResult;
}
}
add_shortcode('inventory', 'inventory');
<div>
<h1>Product ID {$product_id}</h1>
<p>Price $price</p>
</div>
<button value="apple">Get More Info</button>
更多信息
所以我有一个我正在做的项目,但我很难思考如何使用嵌套的短代码。
这是我所拥有的
[inventory store=some_store_id category=fruit]
此短代码当前从数据库返回以下内容[[0]="product_id"=>['name'=>'apple', 'price'=>'2.00'],[1]="another_product_id"=>['name'=>'apple', 'price'=>'2.00']]
我想要这样的东西
<div>
[inventory store=some_store_id category=fruit]
[individual_product]
<div>
<h1>[product_id]</h1>
</div>
<div><h2>[name]</h2></div>
<div><p>[price]</p></div>
[/individual_product]
[/inventory]
</div>