Shortcode function – replacing echo with return – need help
-
Hello,
( I am beginer with creating shortcodes … and still learning php javascript etc…)I managed to make a simple shortcode, and it does what I need, but …
I recently foound out that I should use “return” instead of “echo” if I want to place the shortcode output inside the content of the post and not allways on top.
I went through the code of my function, replaced echo with return, but it doesn’t work (nothing gets returned at all…)
I must be missing something, some difference between echo and return, I guess I can not replace it as simple as that …
Here is the code I put in my functions.php :<?php /** * MY SHORTCODE **/ function display_images_in_list($atts) { extract(shortcode_atts(array( 'velicina' => 'thumbnail', ), $atts)); echo "<div class='slideshow pics'>"; if($images = get_posts(array( 'post_parent' => get_the_ID(), 'post_type' => 'attachment', 'numberposts' => -1, // show all 'post_status' => null, 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC', ))) { foreach($images as $image) { $attimg = wp_get_attachment_image($image->ID,$velicina); $attimgpageurl = get_attachment_link($image->ID); echo "<div class='slajd'><a href='".$attimgpageurl."'>".$attimg."</a></div>"; } } echo "</div><div id='nav'></div>"; } ?> <?php function register_shortcodes(){ add_shortcode('galerija-cycle', 'display_images_in_list'); } ?> <?php add_action( 'init', 'register_shortcodes'); ?>
It works ( for example this is how I used it: [galerija-cycle velicina=”galerija-thumb-2″] )
but shortcode output is always on top of the other content,
even if it is placed inside text of the post.
So I did this:<?php /** * MY SHORTCODE **/ function display_images_in_list($atts) { extract(shortcode_atts(array( 'velicina' => 'thumbnail', ), $atts)); return '<div class="slideshow pics">'; if($images = get_posts(array( 'post_parent' => get_the_ID(), 'post_type' => 'attachment', 'numberposts' => -1, // show all 'post_status' => null, 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC', ))) { foreach($images as $image) { $attimg = wp_get_attachment_image($image->ID,$velicina); $attimgpageurl = get_attachment_link($image->ID); return '<div class="slajd"><a href="'.$attimgpageurl.'">'.$attimg.'</a></div>'; } } return '</div><div id="nav"></div>'; } ?> <?php function register_shortcodes(){ add_shortcode('galerija-cycle', 'display_images_in_list'); } ?> <?php add_action( 'init', 'register_shortcodes'); ?>
As you can see I just replaced echo with return and changed ‘ and ” …
It doesnt work at all – shortcode output is mostly blank …
It does a strange thing: text from post content is ok, it is before the shortcode, but after that the output of the shortcode is just:
<div class=”slideshow pics”>
..missing the images that should be here – instead it somehow wraps two divs from template after shortcode inside this div …
</div>
You help is very much appreciated and needed.
Thanks.
- The topic ‘Shortcode function – replacing echo with return – need help’ is closed to new replies.