Splitting the content got through 'the_content()' in wordpress
[shortcode 1]
[shortcode 2]
[shortcode 3]
[shortcode 4]
[shortcode 5]
[shortcode 6]
Right now these are all static containing only html and javascript content. This post is getting displayed on my index.php using the 'the_content()' template tag.
But now I will be pulling some dynamic content from my other posts in place of some of these shortcodes whose logic will be hardcoded in the index.php file. For eg.-
[shortcode 1] static
[shortcode 2] static
[shortcode 3] dynamic
[shortcode 4] static
[shortcode 5] dynamic
[shortcode 6] static
Just to be clear all the static sections will come through the shortcode but the dynamic sections will be hardcoded in the index.php file. However, because of the serial order this logic is getting messed up.
I want to somehow split up the content coming through 'the_content()' into the appropriate static sections.Sorry for the long post but I hope someone can come up with a solution.
You could create your own shortcode(s) to call the scripts you want to run.
For example, in your functions.php
file you could add something like this:
<?php
function my_shortcode_callback($atts){
$name = attr('name');
switch($name){
case 'script_1':
// run code for script_1, maybe through a custom function
script_1();
break;
case 'script_2':
// run code for script_2, maybe through another custom function
script_2();
break;
}
}
add_shortcode('my_shortcode', 'my_shortcode_callback');
?>
Now, in your post editor, you could add your own shortcode intertwined with the plugin's, such as:
[shortcode 1] static
[shortcode 2] static
[my_shortcode name='script_1'] dynamic
[shortcode 4] static
[my_shortcode name='script_2'] dynamic
[shortcode 6] static
So, basically, you'd be using your plugin for static code and your own shortcode for dynamic code =)
I hope that helps or gets you in the right direction. Let me know!
This is how I solved my problem using normal php explode-
I first added the '~' separator for all my shortcodes like -
[Shortcode1]~[Shortcode2]~[Shortcode3]
Then using the following code I separated the shortcodes-
<?php
ob_start();
the_content();
$content = ob_get_clean();
$part = explode('~', $content);
?>
After that all I did was echoed the required variables at the right spots in the template-
<?php echo $part[0]; ?>,....
精彩评论