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>