1

我知道有办法做到这一点,但是,我很难理解它。这是我的问题。

我有一个短代码,它触发了一个引入商店库存的功能。我对用 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>
4

1 回答 1

0

$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' );

这是我放在内容编辑器中的内容:

[inventory]
  [record]
  //Insert theme buttons using themes builder
  <button value="product_id">Get more info</button>
  [/record]
[/inventory]

这是生成的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>
于 2019-05-23T04:11:58.957 回答