• Resolved chachalady

    (@chachalady)


    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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter chachalady

    (@chachalady)

    I find out this:
    “Replacing echo with return doesn’t work because function is immediately terminated once it hits a return.”

    I thought it must be something I still didn’t learn … So I need help with syntax and coding – how to convert the echo into return.
    I got three echos … On various forum topics I saw that I have to replace all the echo statements with a variable like $stringreturn, then return that variable.
    I am not quite sure how – tried out something already but I guess I made some stupid mistake again…please help

    Thread Starter chachalady

    (@chachalady)

    I found a working code after all! 🙂
    After hours and hours searching and browsing through similar topics I end up with this solution:

    I add :

    ob_start();

    to the first line of the function,

    and at the end of the function I add :

    $output = ob_get_clean();
    return $output;

    And it works well! Shortcode generated output is in the middle of the text where I put it.

    Complete code if someone needs to do similar thing:

    <?php
    /**
    * MY SHORTCODE
    **/
    function display_images_in_list($atts) {
    
    	ob_start(); 
    
    	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>";
      $output = ob_get_clean();
      return $output;
    }
    ?>
    <?php
    function register_shortcodes(){
       add_shortcode('galerija-cycle', 'display_images_in_list');
    }
    ?>
    <?php
    add_action( 'init', 'register_shortcodes');
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Shortcode function – replacing echo with return – need help’ is closed to new replies.