Title: Shortcode displayed BEFORE content
Last modified: December 17, 2019

---

# Shortcode displayed BEFORE content

 *  [joebenett](https://wordpress.org/support/users/joebenett/)
 * (@joebenett)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/shortcode-displayed-before-content/)
 * Hi all,
 * I have a little issue with my shortcodes which displaying the “return content”
   BEFORE other pages contents.
 * ex : hello world [shortcode] –give–> “shortcode return content” hello world
 * Anyone know how to fix please ?
 * Thank you
    -  This topic was modified 6 years, 5 months ago by [joebenett](https://wordpress.org/support/users/joebenett/).
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fshortcode-displayed-before-content%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 7 replies - 1 through 7 (of 7 total)

 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [6 years, 5 months ago](https://wordpress.org/support/topic/shortcode-displayed-before-content/#post-12247225)
 * Please show your code
 *  Thread Starter [joebenett](https://wordpress.org/support/users/joebenett/)
 * (@joebenett)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/shortcode-displayed-before-content/#post-12247276)
 * Here is the function, thank you.
    (this is the first shortcode I do, I think 
   it can be optimized)
 *     ```
       function HP_show_sticky_post() { 
   
       	//créé une nouvelle recherche dans la BDD
       	$sticky_post = new WP_Query();
   
       	//recherche si le post est épinglé
       	$sticky = get_option('sticky_posts');
   
       	//arguments
       	$args = array(
       		'post__in'=> $sticky,
       		'post_type'=>'post',
       		'showposts'=>'1',
       		'ignore_sticky_posts' => 1,
       	);
   
       	//lancement de la recherche avec les arguments	
       	$sticky_post->query($args);
   
       	if ($sticky_post->have_posts()) : while ($sticky_post->have_posts()) : $sticky_post->the_post();?>
       		<div class="hp_sticky_post">
       			<a>post->ID); ?>" id="post-<?php the_ID();?>">
       				<h2><?php echo get_the_title($sticky_post->post->ID); ?></h2>
       			</a>
       			<div class="sp_content">
       				<a>post->ID); ?>" id="post-<?php the_ID();?>">
       					<div class="sp_thumb">
       						<?php if(has_post_thumbnail($sticky_post->post->ID)) {
       							echo get_the_post_thumbnail($sticky_post->post->ID,'');
       						} ?>			
       					</div>
       				</a>
       				<div class="sp_excerpt"><?php echo get_the_excerpt(); ?></div>
       			</div>
       		</div>
       	<?php
       	endwhile; endif;
       }
       ```
   
    -  This reply was modified 6 years, 5 months ago by [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/).
 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [6 years, 5 months ago](https://wordpress.org/support/topic/shortcode-displayed-before-content/#post-12247301)
 * Shortcodes must RETURN a string. You have ECHO statements in there, which mean
   that the echo occurs while the shortcode is being evaluated (i.e., before the
   post is rendered). Stuff all output into a string and return in at the end of
   your function.
 *  Thread Starter [joebenett](https://wordpress.org/support/users/joebenett/)
 * (@joebenett)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/shortcode-displayed-before-content/#post-12247575)
 * Very good to know, thank you.
 * Now I have another “bug”, the posts IDs are displayed on top of the block…
    [https://app.box.com/s/ge7tmhhi514dgsva4mpj9vp01ogy4jwx](https://app.box.com/s/ge7tmhhi514dgsva4mpj9vp01ogy4jwx)
 * Here is the code
 *     ```
       function HP_show_sticky_post() { 
       	$StickyPST = "";
   
       	//créé une nouvelle recherche dans la BDD
       	$sticky_post = new WP_Query();
   
       	//recherche si le post est épinglé
       	$sticky = get_option('sticky_posts');
   
       	//$query = new WP_Query( array( 'post__not_in' => get_option( 'sticky_posts' ) ) );
   
       	//arguments
       	$args = array(
       		'post__in'=> $sticky,
       		'post_type'=>'post',
       		'showposts'=>'1',
       		'ignore_sticky_posts' => 1,
       	);
   
       	//lancement de la recherche avec les arguments	
       	$sticky_post->query($args);
   
       	if ($sticky_post->have_posts()) : while ($sticky_post->have_posts()) : $sticky_post->the_post();
   
       	$sp_id = the_ID();
       	$sp_permalink = get_permalink($sticky_post->post->ID);
       	$sp_title = get_the_title($sticky_post->post->ID);
       	$sp_exerpt = get_the_excerpt();
       	if(has_post_thumbnail($sticky_post->post->ID)) { $sp_thumbnail = get_the_post_thumbnail($sticky_post->post->ID,''); }
   
       	$StickyPST .='	<div class="hp_sticky_post">
   
       						<a href="'.$sp_permalink.'">
       							<h2>'.$sp_title.'</h2>
       						</a>
   
       						<div class="sp_content">
       							<a href="'.$sp_permalink.'">
       								<div class="sp_thumb">'.$sp_thumbnail.' </div>
       							</a>
       							<div class="sp_excerpt">'.$sp_exerpt.'</div>
   
       					</div>';
   
       	endwhile; endif;
       	return $StickyPST;
       }
       add_shortcode("show_sticky_post", "HP_show_sticky_post");
       wp_reset_postdata();
       ```
   
 * _[Moderator note: Please, [No bumping](https://make.wordpress.org/support/handbook/forum-welcome/#do-not%c2%a0bump-posts)._
    -  This reply was modified 6 years, 5 months ago by [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/).
 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [6 years, 5 months ago](https://wordpress.org/support/topic/shortcode-displayed-before-content/#post-12250945)
 * 1. DO NOT BUMP.
 * 2. I”m not seeing where your code might echo. You might have to play around with
   commenting out bits of code.
 * 3. The query reset should be inside your function.
 *  Thread Starter [joebenett](https://wordpress.org/support/users/joebenett/)
 * (@joebenett)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/shortcode-displayed-before-content/#post-12251151)
 * 1. Sorry for the “bump” that will the first and last time (my second help topic)
 * 2. I have only this code on my function file, when I comment or delete it, the
   IDs aren’t displayed.
 * 3. The query reset is now inside my function (the bug is persistant)
 * Thank you for help
 *  Thread Starter [joebenett](https://wordpress.org/support/users/joebenett/)
 * (@joebenett)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/shortcode-displayed-before-content/#post-12253991)
 * Hi all,
 * during code optimization, I found and answering to myself.
 * I just change the
 * > $sp_id = the_ID();
 *  by
 * > $sp_id = get_the_ID();
 *  and voila !
 * Thank you for help 🙂

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Shortcode displayed BEFORE content’ is closed to new replies.

## Tags

 * [display order](https://wordpress.org/support/topic-tag/display-order/)
 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 7 replies
 * 2 participants
 * Last reply from: [joebenett](https://wordpress.org/support/users/joebenett/)
 * Last activity: [6 years, 5 months ago](https://wordpress.org/support/topic/shortcode-displayed-before-content/#post-12253991)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
