WordPress.org

Ready to get started?Download WordPress

Forums

Arconix Shortcodes
[resolved] do_shortcode in theme template (9 posts)

  1. darrenbond
    Member
    Posted 1 year ago #

    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/

  2. John Gardner
    Member
    Plugin Author

    Posted 1 year ago #

    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.

  3. darrenbond
    Member
    Posted 1 year ago #

    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?

  4. John Gardner
    Member
    Plugin Author

    Posted 1 year ago #

    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?

  5. darrenbond
    Member
    Posted 1 year ago #

    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]'); ?>
  6. John Gardner
    Member
    Plugin Author

    Posted 1 year ago #

    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.

  7. darrenbond
    Member
    Posted 1 year ago #

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

  8. laciroc
    Member
    Posted 1 year ago #

    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.

  9. John Gardner
    Member
    Plugin Author

    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic