• So I found this way to add custom field info outside of the loop:http://www.wpbeginner.com/wp-themes/how-to-display-custom-fields-outside-the-loop-in-wordpress/

    Here is the code I’m using to change the default image above my sidebar area:

    <?php
    	    global $wp_query;
    		$postid = $wp_query->post->ID;
    		$tsbi = get_post_meta($post->ID, 'tsbi', true);
    		//Checking if anything exists for the key tsbi "top side bar image"
    		if ($tsbi) { ?>
            <img src="
    		<?php echo $tsbi; ?>
            " width="310" height="157" alt="Sowa Open Market" />
    		<?php }
    		//if there is nothing for tsbi then display
    		else { ?>
    		<img src="<?php bloginfo('stylesheet_directory'); ?>/assets/images/happy-shoppers.png" width="310" height="157" alt="Buy Fresh, Buy Local!" />
    		<?php } ?>

    So basically, the idea is, if there is no custom field data, just show the default image. If there is custom field data, display that image instead (where the value of the custom field key is the url to an image)

    This code works great except on the blog page (homepage displays a static page, “Blog” has been assigned as the “posts page”.

    Here’s a link to the live site:
    http://www.sowaopenmarket.com/blog-2/

    I know it’s probably because there’s a different loop running on the index page than just the normal pages, but I can’t figure out what modification I can make to get this working.

  • The topic ‘Custom fields outside of loop not working on index.php’ is closed to new replies.