Php 有没有办法在插件中嵌套的WordPress短代码之间传递数据?
我知道有一种方法可以做到这一点,但是,我很难理解它。这是我的问题 我有一个短代码,它触发了一个函数,该函数引入了商店库存。我格式化用HTML返回的数据。我的插件已经使用以下短代码Php 有没有办法在插件中嵌套的WordPress短代码之间传递数据?,php,wordpress,shortcode,wordpress-plugin-creation,Php,Wordpress,Shortcode,Wordpress Plugin Creation,我知道有一种方法可以做到这一点,但是,我很难理解它。这是我的问题 我有一个短代码,它触发了一个函数,该函数引入了商店库存。我格式化用HTML返回的数据。我的插件已经使用以下短代码['inventory'] 我想做的是在同一个函数中,如果可能的话,我想创建更多的短代码,例如[product\u id] 希望在我循环记录时,从同一个函数将当前记录的product_id作为该短代码值 还可以将一些WordPress主题元素与shortcode结合使用 假设库存短代码返回以下内容 <div>
['inventory']
我想做的是在同一个函数中,如果可能的话,我想创建更多的短代码,例如[product\u id]
希望在我循环记录时,从同一个函数将当前记录的product_id作为该短代码值
还可以将一些WordPress主题元素与shortcode结合使用
假设库存短代码返回以下内容
<div>
<h1>Product ID {$product_id}</h1>
<p>Price $price</p>
</div>
[inventory]
['record']
//Insert theme buttons using themes builder
<button value=['product_id']>Get more info</button>
['/record]
[/inventory]
此短代码当前从数据库返回以下内容[[0]=“product\u id”=>['name'=>'apple','price'=>'2.00'],[1]=“other\u product\u 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>
[库存商店=某些商店\u id类别=水果]
[个别产品]
[产品编号]
[姓名]
[价格]
[/个别产品]
[/库存]
$content
库存函数包含[inventory]快捷码标记之间的所有内容。您可以执行一些查找和替换代码,将产品id作为属性放入其中,然后对修改后的字符串调用do_shortcode来处理主题生成器添加的任何短代码。我发现product_id周围的括号是有问题的,除非您在那里调用另一个快捷码
function inventory_shortcode_function( $atts, $content = null ){
// do inventory query here
$inventory = [1,2,3];
$output = '<p>There are ' . count( $inventory ) . ' items.</p>';
foreach( $inventory as $item ) {
$loop_content = str_replace( '[record]', '[record product_id="' . $item . '"]', $content );
$output .= '<div>' . do_shortcode( $loop_content ) . '</div>';
}
return $output;
}
add_shortcode('inventory', 'inventory_shortcode_function' );
function record_shortcode_function( $atts, $content = null ){
extract( shortcode_atts([ 'product_id' => -1], $atts ) );
return do_shortcode( str_replace( 'product_id', $product_id, $content ) );
}
add_shortcode('record', 'record_shortcode_function' );
function inventory\u shortcode\u函数($atts,$content=null){
//在这里进行库存查询
$inventory=[1,2,3];
$output='有'.count($inventory.''项。';
foreach($库存为$项目){
$loop_content=str_replace(“[record]”,“[record product_id=“”.$item.”,“$content”);
$output.=''.do_短码($loop_content)。'';
}
返回$output;
}
添加_快捷码(“库存”、“库存_快捷码_函数”);
函数记录\短代码\函数($atts,$content=null){
提取(短码地址(['product\u id'=>-1],$atts));
返回do_短代码(str_replace('product_id',$product_id,$content));
}
添加_短代码(“记录”、“记录_短代码_函数”);
这是我放在内容编辑器中的内容:
[inventory]
[record]
//Insert theme buttons using themes builder
<button value="product_id">Get more info</button>
[/record]
[/inventory]
[目录]
[纪录]
//使用主题生成器插入主题按钮
获取更多信息
[record]
[/库存]
这是生成的html:
<p>There are 3 items.</p>
<div>
//Insert theme buttons using themes builder
<button value="1">Get more info</button></div>
<div>
//Insert theme buttons using themes builder
<button value="2">Get more info</button>
</div>
<div>
//Insert theme buttons using themes builder
<button value="3">Get more info</button>
</div>
共有3项
//使用主题生成器插入主题按钮
获取更多信息
//使用主题生成器插入主题按钮
获取更多信息
//使用主题生成器插入主题按钮
获取更多信息
$content
库存函数包含[inventory]快捷码标记之间的所有内容。您可以执行一些查找和替换代码,将产品id作为属性放入其中,然后对修改后的字符串调用do_shortcode来处理主题生成器添加的任何短代码。我发现product_id周围的括号是有问题的,除非您在那里调用另一个快捷码
function inventory_shortcode_function( $atts, $content = null ){
// do inventory query here
$inventory = [1,2,3];
$output = '<p>There are ' . count( $inventory ) . ' items.</p>';
foreach( $inventory as $item ) {
$loop_content = str_replace( '[record]', '[record product_id="' . $item . '"]', $content );
$output .= '<div>' . do_shortcode( $loop_content ) . '</div>';
}
return $output;
}
add_shortcode('inventory', 'inventory_shortcode_function' );
function record_shortcode_function( $atts, $content = null ){
extract( shortcode_atts([ 'product_id' => -1], $atts ) );
return do_shortcode( str_replace( 'product_id', $product_id, $content ) );
}
add_shortcode('record', 'record_shortcode_function' );
function inventory\u shortcode\u函数($atts,$content=null){
//在这里进行库存查询
$inventory=[1,2,3];
$output='有'.count($inventory.''项。';
foreach($库存为$项目){
$loop_content=str_replace(“[record]”,“[record product_id=“”.$item.”,“$content”);
$output.=''.do_短码($loop_content)。'';
}
返回$output;
}
添加_快捷码(“库存”、“库存_快捷码_函数”);
函数记录\短代码\函数($atts,$content=null){
提取(短码地址(['product\u id'=>-1],$atts));
返回do_短代码(str_replace('product_id',$product_id,$content));
}
添加_短代码(“记录”、“记录_短代码_函数”);
这是我放在内容编辑器中的内容:
[inventory]
[record]
//Insert theme buttons using themes builder
<button value="product_id">Get more info</button>
[/record]
[/inventory]
[目录]
[纪录]
//使用主题生成器插入主题按钮
获取更多信息
[record]
[/库存]
这是生成的html:
<p>There are 3 items.</p>
<div>
//Insert theme buttons using themes builder
<button value="1">Get more info</button></div>
<div>
//Insert theme buttons using themes builder
<button value="2">Get more info</button>
</div>
<div>
//Insert theme buttons using themes builder
<button value="3">Get more info</button>
</div>
共有3项
//使用主题生成器插入主题按钮
获取更多信息
//使用主题生成器插入主题按钮
获取更多信息
//使用主题生成器插入主题按钮
获取更多信息
$content
库存函数包含[inventory]快捷码标记之间的所有内容。您可以执行一些查找和替换代码来将产品id放入其中,删除与此处模板标记非常相似的[record]标记,而不是短代码,然后在修改的字符串上调用do_shortcode
,以处理主题生成器添加的任何短代码。我正在测试并键入一个答案。如果你真的想使用另一个短代码记录并传入id,你可以在短代码字符串上调用do_短代码,如do_短代码([record product_id=4])
那么,当您在记录中传入记录短码并将product_id设置为4时,是否会让记录短码访问product_id值?或者,您是说创建记录短码并为其指定一个product_id属性,然后在do_短码中设置该属性?使用str_替换在第一个短码中设置内容的属性值,然后在该字符串上调用do_短码。现在用代码格式化答案…为此使用add_filter()如何?Shortcode API文档提供了关于嵌套短代码的建议,并提到了添加过滤器作为一种方法。inventory函数的$content
包含[inventory]短代码标记之间的所有内容。您可以执行一些查找和替换代码来将产品id放入其中,删除与此处模板标记非常相似的[record]标记,而不是短代码,然后在修改的字符串上调用do_shortcode
,以处理主题生成器h中的任何短代码