douyong8801 2019-05-23 01:22
浏览 200

有没有办法在插件中的嵌套WordPress短代码之间传递数据?

I know there is a way to do this, however, I am having a hard time understanding it. Here is my problem.

I have a shortcode that triggers a function that brings in a store inventory. I format the data that gets returned with HTML. My plugin does this already using the following shortcode ['inventory']

What I would like to do is within the same function if possible I'd like to create a few more shortcodes such as [product_id]

And hopefully, from the same function place the current records product_id as that shortcode value as I loop through the records.

And also use some of the WordPress theme elements in combination with the shortcode.

So let's say the inventory shortcode returns the following

<div>
    <h1>Product ID {$product_id}</h1>
    <p>Price $price</p>
</div>

and loops through every product, so if there are 4 products it would output the above HTML 4 times.

The theme I am using allows me to create buttons specific to my theme, I don't want to hard code those buttons into my code.

What I want to do is the following

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

So what I would like to do is have the inventory, generate the data to be outputted, but instead of looping through and outputting id like to loop through and pass the data to the ['record'] shortcode and then have that tag render the output with the buttons below each record. And give the button value the product_id shortcode which would hold the current records product ID.

I want to say do_shortcode is involved, but I am not quite sure how to achieve this.

Any help is appreciated

I have tried reading the documentation.

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>

More Info

So I have a project I am working on but am having a hard time wrapping my mind around how to work with nested shortcodes.

Here is what I have

[inventory store=some_store_id category=fruit]

This shortcode currently returns the following from the database [[0]="product_id"=>['name'=>'apple', 'price'=>'2.00'],[1]="another_product_id"=>['name'=>'apple', 'price'=>'2.00']]

Id like to have something like this

<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>
  • 写回答

1条回答 默认 最新

  • dtcrw26206 2019-05-23 04:11
    关注

    $content of your inventory function contains everything between the [inventory] shortcode tags. You can do some find and replace code to put the product id in there as an attribute, and then call do_shortcode on the modified string to handle any shortcodes the themes builder has added. I found that the brackets around product_id were problematic unless you're calling another shortcode there...

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

    This is the content I placed in the content editor:

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

    And this is the generated 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>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法