How can I pass additional params to a shortcode while in a loop?
Question
I am new to WP, and stuck in a shortcode. here’s my issue. created a shortcode like this
add_shortcode( 'position', 'object_position_in_post');
function object_position_in_post( $atts = [] ) {
$atts = array_change_key_case( (array) $atts, CASE_LOWER );
$format = $atts['format'];
$position = get_post_meta( $product, '_target_position'.$post->ID, true);
if(isset($atts['format']) && !empty($atts['format'])){
if($format === 'num')
return $position;
if($format === 'word')
$formatter = new NumberFormatter('en_US', NumberFormatter::SPELLOUT);
return str_replace('-', ' ', $formatter->format($position));
}
return &position;
}
I need to use it on posts where multiple products are called in blocks using a loop; so how can I pass addition params so that it becomes dynamic and works flawlessly. shortcode should become something like this
[position format="what-was-passed" product_id='".$product->ID."' postid='".$post->ID."']
Any help is highly appreciated;
0
1 year
2021-11-24T01:07:59-05:00
2021-11-24T01:07:59-05:00 0 Answers
0 views
0
Leave an answer