WordPress.org

Ready to get started?Download WordPress

Forums

Return doesn't put variable where it belongs (6 posts)

  1. mortalwombat
    Member
    Posted 2 years ago #

    I have created a function that returns a string of HTML. Problem is, I am using bloginfo("template_url") to get the path to an images folder inside my theme. The result of bloginfo() gets output outside the HTML created by the function. Why is this, and how do I fix it?

    Here is my code:

    function build_upperfooter() {
    	$upperfooter_output = '
    		<div id="upper_footer">
    			<div id="upper_footer_content">
    				<ul>
    					<li class="social"><img src="' . bloginfo("template_url") . '/images/facebook_icon.png" alt="visit our facebook page" /><a targe="_blank" href="http://www.facebook.com/mattandjentry">Facebook</a></li>
    					<li class="social"><img src="' . bloginfo("template_url") . '/images/twitter_icon.png" alt="visit our twitter feed" /><a targe="_blank" href="http://twitter.com/#!/mattandjentry">Twitter</a></li>
    					<li class="social"><img src="' . bloginfo("template_url") . '/images/pinterest_icon.png" alt="visit our pinterest boards" /><a targe="_blank" href="http://pinterest.com/jentrydryden/">Pinterest</a></li>
    					<li class="address">1451 Alvin Ct. * Sparks, NV 89434 | 775.332.9807</li>
    				</ul>
    			</div>
    			<p class="copyright">&copy; <?php echo date("Y") ?> Matt and Jentry: Photographers, LLC</p>
    		</div>';
    	return $upperfooter_output;
     }

    and here is a link to my dev site which currently shows the results: http://mattandjentry.com/test/weddings/

  2. mortalwombat
    Member
    Posted 2 years ago #

    Here's a little more info I have found when trying figure out what's going on: I simplified the function DRASTICALLY to this:

    function build_upperfooter() {
    
    	$wtf = bloginfo("template_url");
    
     }

    Now, I wouldn't expect this to really do anything except define the variable, but it is printing it onto the page. Why is that?!

  3. mortalwombat
    Member
    Posted 2 years ago #

    Also note, if I hard code the string into the variable, things work fine:

    function build_upperfooter() {
    	$wtf = "http://mattandjentry.com/test/wp-content/themes/_mattandjentry2";
    	$upperfooter_output = '
    		<div id="upper_footer">
    			<div id="upper_footer_content">
    				<ul>
    					<li class="social"><img src="' . $wtf . '/images/facebook_icon.png" alt="visit our facebook page" /><a targe="_blank" href="http://www.facebook.com/mattandjentry">Facebook</a></li>
    					<li class="social"><img src="' . $wtf . '/images/twitter_icon.png" alt="visit our twitter feed" /><a targe="_blank" href="http://twitter.com/#!/mattandjentry">Twitter</a></li>
    					<li class="social"><img src="' . $wtf . '/images/pinterest_icon.png" alt="visit our pinterest boards" /><a targe="_blank" href="http://pinterest.com/jentrydryden/">Pinterest</a></li>
    					<li class="address">1451 Alvin Ct. * Sparks, NV 89434 | 775.332.9807</li>
    				</ul>
    			</div>
    			<p class="copyright">&copy; <?php echo date("Y") ?> Matt and Jentry: Photographers, LLC</p>
    		</div>';
    	return $upperfooter_output;
     }
  4. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    using bloginfo("template_url") to get the path to an images folder

    read the codex chapter on 'bloginfo':
    http://codex.wordpress.org/Function_Reference/bloginfo

    If you need the values for use in PHP, use get_bloginfo().

    many wordpress functions have a version starting with get_ which returns the value;
    a few examples:

    bloginfo() -vs- get_bloginfo()
    the_permalink() -vs- get_permalink()
    the_title() -vs- get_the_title()

    if in doubt or when you are having problems with a function, consult the codex.

  5. Digital Raindrops
    Member
    Posted 2 years ago #

    get the path to an images folder inside my theme

    To get files from within a child themes folder use

    <?php /* Start include file by file path */ ?>
    $file_path = get_stylesheet_directory() . '/inc/meta-functions.php';
    if( file_exists( $file_path ) ) include_once( $file_path );
    <?php /* End include file by file path */ ?>

    To get images to display on a template page from within a parent themes folder

    <?php /* Start get image by url */ ?>
    <?php $img_url = get_template_directory_uri() . '/images/my-image.png'; ?>
    <img src="<?php echo $img_url; ?>" alt="" title="" width="" height="" />
    <?php /* End get image by url */ ?>

    Parent Themes Folder
    get_template_directory()
    get_template_directory_uri()

    If we are using a child theme then TEMPLATEPATH becomes STYLESHEETPATH
    Child Themes Folder
    get_stylesheet_directory()
    get_stylesheet_directory_uri()

    These are very handy WordPress function wrappers, which in turn call get_bloginfo() with the correct arguments, returning the path or url.

    HTH

    David

  6. mortalwombat
    Member
    Posted 2 years ago #

    Thank you guys so so so much! That did the job. I'm clearly going to have to read up more on the different wp functions.

Topic Closed

This topic has been closed to new replies.

About this Topic