AliMahachkala
AliMahachkala

Reputation: 5

Shortcode code wraps content code

I've created a shortcode with list of links. The problem is- blog post content that follows inserted shortcode ends up inside shortcode wrapping divs (.customwrap and .customcontainer inside the code). List of links is actually a custom post type with custom taxonomies. Displayed taxonomy is determined by one shortcode attribute (placement). There is also dynamic class which is determined by another attribute (position).

Here is the code.

function custom_links_code($atts) {
    ob_start();
   extract(shortcode_atts(array(
      'placement' => 'test',
      'position' => 'horizontal'
   ), $atts)); 
?>

<!-- content following shortcode ends up inside these divs -->   
<div class="customwrap <?php echo $atts['position']; ?>">
<div class="customcontainer">

<?php

  $args = array(
  'post_type'   => 'links',
  'post_status' => 'publish',
  'tax_query'   => array(
    array(
        'taxonomy' => 'Placements',
        'field'    => 'slug',
        'terms'    => $atts['placement']
    )
  )
 );

$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
 ?>

<a href="<?php echo get_post_meta(get_the_ID(), 'link-url', true); ?>"> 
 <?php the_title(); ?>
 </a>
 <?php 
endwhile;
wp_reset_query();
return ob_get_clean();
} 
echo '</div></div>';
add_shortcode('custom-links', 'custom_links_code');

Upvotes: 0

Views: 33

Answers (1)

Kallmanation
Kallmanation

Reputation: 1182

Looks like you need to move your closing divs into your function:

...
<?php the_title(); ?>
</a>
<?php endwhile; ?>
</div></div> 
<?php // ^^---- This closes our divs ?>
wp_reset_query();
return ob_get_clean();
} 

add_shortcode('custom-links', 'custom_links_code');

Upvotes: 1

Related Questions