WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Tab shortcode with custom fields (16 posts)

  1. Corbula
    Member
    Posted 2 years ago #

    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?

    Thanks

  2. Rev. Voodoo
    Volunteer Moderator
    Posted 2 years ago #

    <?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

  3. Corbula
    Member
    Posted 2 years ago #

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

    Does it have to be on the same line?

  4. Rev. Voodoo
    Volunteer Moderator
    Posted 2 years ago #

    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.

  5. Corbula
    Member
    Posted 2 years ago #

    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]

  6. Corbula
    Member
    Posted 2 years ago #

  7. Peter Baylies
    Member
    Posted 2 years ago #

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

    http://pastebin.com/PrT5hVS7

  8. Corbula
    Member
    Posted 2 years ago #

    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?

  9. Peter Baylies
    Member
    Posted 2 years ago #

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

    http://pastebin.com/6unjXyZu

    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.

  10. Corbula
    Member
    Posted 2 years ago #

    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?

  11. Peter Baylies
    Member
    Posted 2 years ago #

    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!

  12. Corbula
    Member
    Posted 2 years ago #

    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.

  13. Corbula
    Member
    Posted 2 years ago #

    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.

    http://pastebin.com/qduevTMQ

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

    http://wordpress.org/extend/plugins/put/

    I'm stumped with what else i can try.

  14. Corbula
    Member
    Posted 2 years ago #

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

    http://wordpress.org/extend/plugins/wp-ui/

  15. Peter Baylies
    Member
    Posted 2 years ago #

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

  16. Mark / t31os
    Moderator
    Posted 2 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic