Support » Fixing WordPress » Retrieve custom field value from a page (not post)

  • Resolved keithiopian

    (@keithiopian)


    y home page has an image which populates via a URL inserted into the custom field I’ve created. My snippet of code is displaying my img tag 3 times, first 2 with no src, (i.e. <img src with=900>), and the 3rd properly. It even eliminats the equals sign and the 2 double quotes. This is the snippet I am referring to:

    <main id="home">
    	<?php query_posts('post_type=page'); ?>
    	<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        	<img id="showcase" src="<?php echo get_post_meta($post->ID, 'Main Home Page Image',true); ?>" width="900">
    	<?php endwhile; ?>
            <?php else : ?>
            <h2>Not Found</h2>
    	<?php endif; ?>
            <?php wp_reset_query(); ?>
        </main>

    When this renders in the browser, I get 2 blank squares and the 3rd is the image I want. Source code when rendered looks like this:

    <img id="showcase" src width="900">
    <img id="showcase" src width="900">
    <img id="showcase" src="http://localhost/wp-content/uploads/2014/04/index.jpg" width="900">

    I am simply looking to pull the URL once and have it populate the image. Again, this is coming from a page not a post. Can anyone offer me some insight?

Viewing 4 replies - 1 through 4 (of 4 total)
  • there is possibly no need for the query and loop;

    try simply:

    <main id="home">
    	<?php if( is_front_page() && is_page() && get_post_meta($post->ID, 'Main Home Page Image',true) ) : ?>
        	<img id="showcase" src="<?php echo get_post_meta($post->ID, 'Main Home Page Image',true); ?>" width="900">
    	<?php endif; ?>
        </main>

    http://codex.wordpress.org/Conditional_Tags

    Thanks for the thought alchymyth, unfortunately that makes everything between the <main> tag disappear leaving me just this:

    <main id="home">
            </main>

    the code was assuming that you have set a static home page;
    you could try to simplify the conditionals to:

    <?php if( is_front_page() && get_post_meta($post->ID, 'Main Home Page Image',true) ) : ?>

    if you don’t have a static front page, go back to your code and check for the existence of the custom field content before outputting the image tag;

    <main id="home">
    	<?php query_posts('post_type=page'); ?>
    	<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        	<?php if( get_post_meta($post->ID, 'Main Home Page Image',true)) { ?>
        	<img id="showcase" src="<?php echo get_post_meta($post->ID, 'Main Home Page Image',true); ?>" width="900">
        	<?php } ?>
    	<?php endwhile; ?>
            <?php else : ?>
            <h2>Not Found</h2>
    	<?php endif; ?>
            <?php wp_reset_query(); ?>
        </main>

    this should possibly work as long as you have only one page with the custom field filled in and less pages than the number of ‘posts per page’ set in the dashboard.

    Thanks alchymyth! Didn’t need the loop, the slimmed down code worked like a charm.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Retrieve custom field value from a page (not post)’ is closed to new replies.