Creating a shortcode that would pull a certain custom post type. Using the_title() for test purposes. The shortcode itself is working fine (echoing an attribute value). Query when used outside a shortcode works fine as well. But when it comes down to querying the post type inside the shortcode, at first, it takes quite a bit of time to load. And when it finaly does, it repeats the shortcode attribute with previously used shortcode many-many times. Title doesn't show. So, Let's say I'm using 2 shortcodes on the same page.
Shortcode 1 (the problematic one):
[custom-table custom="here we are gonna have a slug"]
Shortcode 2:
[custom-button width="300px" bg="#29b938" color="#ffffff" padding="25px 35px" text="Start Your Diet" font="25px" weight="bold" style="" url="http://google.com" new-tab="true"]
code for shortcode 1
function tables_shortcode($atts) {
ob_start();
extract(shortcode_atts(array(
'custom'=> 'none'
), $atts));
$tabargs = array(
'posts_per_page' => -1,
'offset' => 0,
'post_type' => 'customtables'
);
$tabs = new WP_Query($tabargs);
if ( have_posts() ) while ($tabs->have_posts()) : $tabs->the_post();
the_title();
endwhile;
wp_reset_postdata();
echo $atts['custom'];
return ob_get_clean();
}
add_shortcode('custom-table','tables_shortcode');
code for shortcode 2 (works fine on its own)
function custom_button($atts) {
ob_start();
extract(shortcode_atts(array(
'width' => '200',
'bg' => '#000',
'color' => '#fff',
'padding' => '10px 20px',
'text' => 'Visit Now',
'font' => '20px',
'weight' => '300',
'style' => 'normal',
'url' => '#',
'new-tab'=> 'false'
), $atts));
?>
<div class="inrevbtn">
<a class="custombtn" href="<?php echo $atts['url'];?>" <?php if ($atts['new-tab'] == 'true') { echo 'target="_blank"'; } ?>>
<?php echo $atts['text']; ?>
</a>
</div>
<?php
return ob_get_clean();
}
add_shortcode('custom-button', 'custom_button');