WordPress.org

Support

Support » Plugins and Hacks » Arconix Shortcodes » [Resolved] do_shortcode in theme template

[Resolved] do_shortcode in theme template

  • Hi,

    Is it possible to use the ‘echo do_shortcode’ function in my theme to use Tabs? I can’t seem to get it to work, and I’m placing it inside the loop.

    This is my template:

    <?php get_header(); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    <div class="post" id="post-<?php the_ID(); ?>">
    
    <h1><?php the_title(); ?></h1>
    
    <?php the_content(); ?>
    
    <?php echo do_shortcode('[tabs]'); ?>
    
    	<?php echo do_shortcode('[tab title="Test1"]'); ?>
    	<p>Content here</p>
    	<?php echo do_shortcode('[/tab]'); ?>
    
    	<?php echo do_shortcode('[tab title="Test2"]'); ?>
    	<p>Content here</p>
    	<?php echo do_shortcode('[/tab]'); ?>
    
    <?php echo do_shortcode('[/tabs]'); ?>
    
    </div>
    
    <?php endwhile; endif; ?>
    <?php get_footer(); ?>

    Any suggestions? Thanks.

    http://wordpress.org/extend/plugins/arconix-shortcodes/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author John Gardner

    @jgardner03

    Hi Darren,

    They absolutely should be able to work via template tags. I’m thinking the issue might be related to the breaking up of the tabs into multiple do_shortcode()‘s.

    I haven’t tested it to make sure, but I think the following would work:

    <?php
    $t = '[tabs][tab title="Tab Title"]
    <p>Tab Content here</p>
    [/tab]
    [tab Title="Tab 2 Title"]
    <p> Tab Content here</p>
    [/tab][/tabs]';
    
    echo do_shortcode( $t );

    It just puts all the shortcode related info into a variable, and then we execute the entire block in a single do_shortcode() call.

    Give that a shot and let me know how you make out.

    Hi John,

    Thanks, that worked.

    My problem now is that I will need to pull content from a custom field (get_post_meta) as the tab content for multiple tabs. Any idea how I can go about that?

    Plugin Author John Gardner

    @jgardner03

    do you know how to access the custom field info and are unsure how to build the tab info, or do you need help getting at your custom fields?

    Sorry, I should have made myself clear; I already know how to retrieve custom field data (I’m using the ACF plugin) I’m unsure how to use it inside of the variable you described above so that I can use your Tabs shortcode.

    Ideally I’d like to use an ‘if’ statement so that a tab is only created if the custom field is populated. I had an idea of how to do it using individual do_shortcode()’s, but as discussed that’s currently not an option.

    This was to be my code:

    <?php echo do_shortcode('[tabs]'); ?>
    
    	<?php
    	if ( get_field('service1')) {
    	echo do_shortcode('[tab title"Service 1"]');
    	echo '<h1>Service 1</h1>';
    	echo get_field('service1');
    	echo do_shortcode('[/tab]');
    	} ?>
    
    	<?php
    	if ( get_field('service2')) {
    	echo do_shortcode('[tab title"Service 2"]');
    	echo '<h1>Service 2</h1>';
    	echo get_field('service2');
    	echo do_shortcode('[/tab]');
    	} ?>
    
    	<?php
    	if ( get_field('service3')) {
    	echo do_shortcode('[tab title"Service 3"]');
    	echo '<h1>Service 3</h1>';
    	echo get_field('service3');
    	echo do_shortcode('[/tab]');
    	} ?>
    
    	<?php
    	if ( get_field('service4')) {
    	echo do_shortcode('[tab title"Service 4"]');
    	echo '<h1>Service 4</h1>';
    	echo get_field('service4');
    	echo do_shortcode('[/tab]');
    	} ?>
    
    <?php echo do_shortcode('[/tabs]'); ?>
    Plugin Author John Gardner

    @jgardner03

    You’re actually not that far off… something like this should get you going:

    <?php
    $t = '[tabs]';
    
    if ( get_field('service1')) {
        $t .= '[tab title="Service 1"]';
        $t .= '<h1>Service 1</h1>';
        $t .= get_field('service1');
        $t .= '[/tab]';
    }
    
    if ( get_field('service2')) {
        $t .= '[tab title="Service 2"]';
        $t .= '<h1>Service 2</h1>';
        $t .= get_field('service2');
        $t .= '[/tab]';
    }
    
    $t .= '[/tabs]'; 
    
    echo do_shortcode( $t ); ?>

    *Edit* After looking at the code again, I removed an unnecessary secondary variable and just did everything through $t.

    That is absolutely perfect. Thank you so much for your quick and helpful replies.

    I am trying to show some posts in tabs and used part of the code shown above, however, the <p> are showing above the tabs and the links are not working. Any ideas about what I am doing wrong?

    <?php
    
    $t = '[tabs][tab title="Tab Title"]';
    if (have_posts()) : while (have_posts()) : the_post();
    $t.= '<p><a href="'.the_permalink().'">'. the_title().'</a></p>
    [/tab]
    [tab Title="Tab 2 Title"]
    <p> Tab Content here</p>
    [/tab][/tabs]';
    endwhile; endif;
    echo do_shortcode( $t );
    ?>

    Thanks.

    Plugin Author John Gardner

    @jgardner03

    Hi laciroc,

    Thanks for using my plugin. Without knowing the context of what you’re trying to achieve, I can’t really provide any guidance. If you could give me some details of what you’re expecting to happen, I should be able to help you out.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘[Resolved] do_shortcode in theme template’ is closed to new replies.
Skip to toolbar