Support » Plugins » Hacks » Tab shortcode with custom fields

  • Resolved Corbula


    I’ve got a template file which uses custom field and i want to use shortcodes in the file as well.

    The shortcode makes the content appear in tabs on the page.

    I’ve had a go but i’m not a php developer. It comes up with an error saying Unexpected T_STRING. The error comes on the first do_shortcode line.

    Here’s the code:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    			<h1><?php the_title(); ?></h1>
                	<?php do_shortcode("[tab name='Overview']<div id="product-image"><?php the_post_thumbnail(); ?></div>
    				<?php the_field('overview'); ?>[/tab]");
                    do_shortcode("[tab name='Features & Benefits']<?php the_field('features_&_benefits'); ?>[/tab];");
                    do_shortcode("[tab name='Options & Accessories']<?php the_field('options_&_accessories'); ?>[/tab];");
                    do_shortcode("[tab name='Downloads']<a href="<?php the_field('downloads'); ?>">Download PDF</a>[/tab];");
                    do_shortcode("[tab name='Supplies']<?php the_field('supplies'); ?>[/tab];");
                    echo do_shortcode("[end_tabset]");?>

    Anybody know how to get this to work?


Viewing 15 replies - 1 through 15 (of 15 total)
  • <?php do_shortcode("[tab name='Overview']<div id="product-image"><?php the_post_thumbnail(); ?></div>

    Well, looking at that code…

    <?php do_shortcode("[

    has an opening php tag, bracket, and an opening double quote, there are no closing php tag, bracket, or double quote

    That’s because the line below is supposed to be part of the same tab.

    Does it have to be on the same line?

    That won’t work because here

    <?php do_shortcode("[tab name='Overview']<div id="product-image"><?php

    you nest php

    You have an opening php tag, and then another opening php tag … you can’t open a php command if it’s already open.

    The reason for that is to encapsulate the fields in the shortcode. Notice the [/tab].

    I’ve tried this, it loads the page but does use tabs like i’m trying to get the shortcodes to do.

    [Code moderated as per the Forum Rules. Please use the pastebin]

    Quotes in PHP don’t work the way you want them to; I would suggest using PHP output buffering for this instead:

    Thanks for that however, it’s not working and i’m not familiar with the syntax of output buffering. When using output buffering do you not need do_shortcode?

    It’s just displaying the page but putting ob_start() and [tab name] and things in the actual page content.

    Is it missing anything from the code?

    Yes, for some reason a couple of PHP tags didn’t show up; see if this works better:

    Also note that do_shortcode() is still there at the bottom, the only difference is that we’re putting that section of the page into the $output_buffer variable, using PHP’s output buffering. This avoids the quoting issues you were running into before.

    Thanks but there’s still something wrong.

    The page loads but it’s still not loading in tabs and it’s printing all of the [tab name=””] and [/tabs].

    Should there not be some php around them or something so it doesn’t just print them on the page?

    If you’re using that code in your template, then that sounds like an issue with your [tab] shortcode (it is [tab] and not [tabs]?). Test those shortcodes on a page and verify that they work on the site. The PHP that executes them would be the do_shortcode() at the bottom. Good luck!

    Yes i’m trying to use it in a template file so it does it automatically and so you don’t have to use them every time you post something.

    It works in a post but the post type i’m using has multiple editors. If i try and use it across multiple editors it doesn’t work. I’m assuming it’s because i’m also using the featured image in the template and because you can’t add a shortcode to that it fails the whole page.

    That’s why i want to use it in the template so it can do all of the content and do it automatically.

    I’ve not been able to get your code to work. I’ve tried numerous different tweaks with your code and i’ve not got it to work.

    I’ve also tried the following, which may be wrong but i thought the . in the code might work instead of using multiple <?php tags.

    The plugin i’m using for the tabs is this.

    I’m stumped with what else i can try.

    Just to let you know i’ve managed to do this using a different plugin.

    Great, glad to hear it; sorry the other one didn’t work out for you.

    Sorry i wasn’t here to help you along sooner, but i can see a couple of issues in the way you were approaching your custom code.

    Firstly, the tab names/titles do not support HTML(that info does exist in the plugin’s FAQ page), those names have to be sanitized seperately and i considered it unsafe to let users post HTML inside there and running the content through kses would have only slowed down the plugin’s operation. It’s certainly something i could look at again, but i felt it was the right choice initially when i made the call(no reason i can’t re-evaluate that decision though).

    Secondly, the_field is not a WP function, so i assume specific to your theme(or a plugin), but in any case it may not give a return value, which i think you’ll need if using it inside do_shortcode, same applies to the_post_thumbnail which would be need to be replaced with get_the_post_thumbnail (for a return value vs a printed one).

    In any case, i’m glad to hear you were able to archieve what you wanted, and sorry i wasn’t available to help sooner, best of luck with your WordPress site.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Tab shortcode with custom fields’ is closed to new replies.