Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

All, I'm trying to create a custom template to display my single blog posts and I'm trying to use Shortcodes to create a tabbed layout. Say I have the following code to display some tabs:

<?php
echo do_shortcode('[tabs][tab title="Tab 1"]This is text[/tab][tab title="Tab2"]Tab content...[/tab][/tabs]'); 
?>

This works fine, however say I'd like to add in some dynamic content into my second tab with a query.

I tried to do something like this:

<?php
$shortcode = do_shortcode('[tabs][tab title="Tab 1"]This is text[/tab][tab title="Tab2"]'); 
$post_id = $post->ID;
$qry = "Select * from table where ID='$post_id'";
$result = mysql_query($qry);
$resultset = mysql_fetch_array($result);
$shortcode .= "The first field is: ".$resultset['field1'];
$shortcode .= do_shortcode('[/tab][/tabs]');
echo $shortcode;
?>

However this didn't work. I can't think of any other way to add some custom dynamic content into a shortcode. Can anyone else give me some ideas?

Thanks in advance

share|improve this question

closed as not a real question by toscho Jan 2 at 23:11

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, see the FAQ.

1 Answer

Should you maybe create the parameter that you will pass to do_shortcode() first?

something like....

$resultset = mysql_fetch_assoc(mysql_query("SELECT * FROM table WHERE ID='{$post->ID}"));

$content = '[tabs][tab title="Tab 1"]This is text[/tab][tab title="Tab2"]';
$content .= 'The first field is: ' . $resultset['field1'];
$content .= '[/tab][/tabs]';

$shortcode = do_shortcode($content);
echo $shortcode;
share|improve this answer
That's not a bad idea but unfortunately it doesn't work. – user1048676 Feb 29 '12 at 6:56
seems it would.. although i missed a parenthesis. – J_B Mar 1 '12 at 1:02
YoOu should use the $wpdb; class for queries. Currently you leave your door open for cross scripting attacks. – kaiser Sep 30 '12 at 18:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.