PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

content template and shortcodes/php

  1. As usual--love the plugin/love the work--thank you thank you.

    I am working on a pretty basic form--8 fields that all build out a content template. The content template includes some HTML for the formatting of the page when it builds out. I am inserting two shortcodes as part of my page structure. Everything is great, form works pages build out etc., etc.

    The two shortcodes are calling in two separate random image functions. The functions work, images display everything mostly works except that the images both display at the absolute top of the the_content(); regardless of where the shortcode is inserted and regardless of the CSS.

    URL--of course, password protected/client confidential--etc., etc.

    Random Image Code (functions.php):

    function random_city_second_image() {
    	$dir = dirname(__FILE__)."/images/city_page_second/";
    	if (is_dir($dir)) :
    		$dir_list = array_diff(scandir($dir), array(".", "..", ".svn"));
    		$dir_list = array_values($dir_list);
    	$output .= "<img src='".WP_CONTENT_URL."/themes/zyzyx/images/city_page_second/{$dir_list[mt_rand(0,count($dir_list)-1)]}' >";
    	echo $output;
    	endif;
    }
    add_shortcode('citypageimage2', 'random_city_second_image');

    Template Structure

    <div class="city_fourth">[citypageimage1]<p>{Intro Paragraph (after phone number):18}</p></div>
    <div class="one-one"><p>{Paragrah Before bullets:17}</p>
    {Bullet list:16:}
    {Text Following Bullets:4}</div>
    <div><p class="city_third" >[citypageimage2]
    {Blue Font--Next to Image:8}</p></div>
    {Final Paragraph:23}

    Other shortcodes I have tried (ones that only add in styles) display correctly.

    I tried simply including the javascript/php call for the function and that--obviously--does not work...however the commented out code displays is in the correct position (next to "Blue Font").

    Any ideas why the random image shortcode would both be displaying at the top of the content area?

    Posted 7 years ago on Monday April 9, 2012 | Permalink
  2. kyle
    Member

    Change

    echo $output;

    To:

    return $output

    Shortcodes have to be returned not echoed.

    Posted 7 years ago on Tuesday April 10, 2012 | Permalink
  3. Kyle--I never replied, sorry about that. Thank you for the comment--helps a ton.

    Posted 7 years ago on Thursday June 14, 2012 | Permalink

This topic has been resolved and has been closed to new replies.