• 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 4 years, 11 months ago by joebenett.

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Please show your code

    Thread Starter joebenett

    (@joebenett)

    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;
    }
    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    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

    (@joebenett)

    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

    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.

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    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

    (@joebenett)

    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

    (@joebenett)

    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.