[shortcode]
[shortcode1 title="variable text"] Some text[/shortcode1]
[shortcode1 title="variable text"] Some text[/shortcode1]
[shortcode1 title="variable text"] Some text[/shortcode1]
[shortcode1 title="variable text"] Some text[/shortcode1]
[shortcode1 title="variable text"] Some text[/shortcode1]
[shortcode1 title="variable text"] Some text[/shortcode1]
[/shortcode]
Based on the need for a shortcode an HTML parts needs to be repeated based on user/admin inputs. I have presented a format. Is the above a possibility?
Can a shortcode be published within a shortcode multiple times?
Further explanation of the situation Future reader may find it helpful→ This is the edifice of an HTML (Hypothetical example)
<div class="classone">
<div class="classtwo">
<div class="classthree">
<ul class="four">
<li class="question">
<h3>Question</h3>
</li>
<li class="answer">
<p>Answer</p>
</li>
</ul>
</div>
</div>
</div>
This part is repetitive and Inner shortcode →
<li class="question">
<h3>Question</h3>
</li>
<li class="answer">
<h3>Question</h3>
</li>
I think the code for the external shortcode will be written like this →
function external_shortcode(){
ob_start();
?>
<div class="classone">
<div class="classtwo">
<div class="classthree">
<ul class="four">
<!-- This is the place where I want the inner shortcode many time -->
</ul>
</div>
</div>
</div>
<?php
return ob_get_clean();
}
add_shortcode( 'shortcode', 'external_shortcode' );
Code for the Inner shortcode →
function internal_shortcode($atts, $content = null){
$data = [
'title' => 'Some heading text goes here.',
];
$values = shortcode_atts($data, $atts);
ob_start();
?>
<li class="question">
<h3><?php echo esc_attr($values['title']); ?></h3>
</li>
<li class="answer">
<p><?php echo $content; ?></p>
</li>
<?php
return ob_get_clean();
}
add_shortcode( 'shortcode1', 'internal_shortcode' );
[shortcode]
[shortcode1 title="variable text"] Some text[/shortcode1]
[shortcode1 title="variable text"] Some text[/shortcode1]
[shortcode1 title="variable text"] Some text[/shortcode1]
[shortcode1 title="variable text"] Some text[/shortcode1]
[shortcode1 title="variable text"] Some text[/shortcode1]
[shortcode1 title="variable text"] Some text[/shortcode1]
[/shortcode]
Based on the need for a shortcode an HTML parts needs to be repeated based on user/admin inputs. I have presented a format. Is the above a possibility?
Can a shortcode be published within a shortcode multiple times?
Further explanation of the situation Future reader may find it helpful→ This is the edifice of an HTML (Hypothetical example)
<div class="classone">
<div class="classtwo">
<div class="classthree">
<ul class="four">
<li class="question">
<h3>Question</h3>
</li>
<li class="answer">
<p>Answer</p>
</li>
</ul>
</div>
</div>
</div>
This part is repetitive and Inner shortcode →
<li class="question">
<h3>Question</h3>
</li>
<li class="answer">
<h3>Question</h3>
</li>
I think the code for the external shortcode will be written like this →
function external_shortcode(){
ob_start();
?>
<div class="classone">
<div class="classtwo">
<div class="classthree">
<ul class="four">
<!-- This is the place where I want the inner shortcode many time -->
</ul>
</div>
</div>
</div>
<?php
return ob_get_clean();
}
add_shortcode( 'shortcode', 'external_shortcode' );
Code for the Inner shortcode →
function internal_shortcode($atts, $content = null){
$data = [
'title' => 'Some heading text goes here.',
];
$values = shortcode_atts($data, $atts);
ob_start();
?>
<li class="question">
<h3><?php echo esc_attr($values['title']); ?></h3>
</li>
<li class="answer">
<p><?php echo $content; ?></p>
</li>
<?php
return ob_get_clean();
}
add_shortcode( 'shortcode1', 'internal_shortcode' );
Share
Improve this question
edited Sep 16, 2019 at 20:05
WordCent
asked Sep 16, 2019 at 18:12
WordCentWordCent
1,8916 gold badges34 silver badges60 bronze badges
2 Answers
Reset to default 3Yes, you can include a shortcode however many times you like, inside a shortcode. Just make sure you set up the parent shortcode to parse shortcodes nested within.
So, for example, if your [shortcode]
does something like
function outer_shortcode($content = null) {
return '<div>' . $content . '</div>';
}
just be sure to update it to parse the content for other shortcodes:
function outer_shortcode($content = null) {
return '<div>' . do_shortcode($content) . '</div>';
}
You can then nest however many duplicate shortcodes as you like inside.
Using @WebElaine's example, all you need is to add the do_shortcode($content) to your current code:
// use both parameters in your function declaration, even if you will ignore the $atts
function external_shortcode($atts, $content){
ob_start();
?>
<div class="classone">
<div class="classtwo">
<div class="classthree">
<ul class="four">
<?php
// Using do_shortcode will cause Wordpress to output your inner shortcodes.
do_shortcode($content);
?>
</ul>
</div>
</div>
</div>
<?php
return ob_get_clean();
}
add_shortcode( 'shortcode', 'external_shortcode' );