treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Need alternative to get_post_meta for custom fields on Blog page

  • I'm using the get_post_meta function to call and echo custom fields used on 'Pages' with a WP site. However, on the 'Blog' page this method is not working. I'm guessing that it's because it's not a 'page' but a group of posts.

    Does anyone have any suggestions on how to call and echo these custom fields on the the 'blog' page as well as the single and archived pages for blog posts? Below is the code I'm using in the 'header'.

    snorkel.jerryrossphotography.com

    <body <?php body_class(); ?>>

    <?php $backgroundPath = get_post_meta($post->ID, 'Background', true);?>

    <img class="bg" src="<?php bloginfo ('template_url'); ?>/images/<?php echo $backgroundPath ?>" border="0">

    <div id="page-wrap">

    <header>

    <div id="logo">
    <a href="http://snorkel.jerryrossphotography.com/&quot; title="Logo for Coral Reef Divers, Kailua-Kona, Hawaii"><img src="<?php bloginfo ('template_url'); ?>/images/logo-coral-reef-divers.png" alt="Link to Home Page of Coral Reef Divers, Kailua-Kona, Hawaii" border="0"></a>
    </div>

    <?php $MastheadImage01Path = get_post_meta($post->ID, 'Masthead-Image-01', true);?>
    <?php $MastheadImage02Path = get_post_meta($post->ID, 'Masthead-Image-02', true);?>
    <?php $MastheadImage03Path = get_post_meta($post->ID, 'Masthead-Image-03', true);?>
    <?php $MastheadThumb01Path = get_post_meta($post->ID, 'Masthead-Thumb-01', true);?>
    <?php $MastheadThumb02Path = get_post_meta($post->ID, 'Masthead-Thumb-02', true);?>
    <?php $MastheadThumb03Path = get_post_meta($post->ID, 'Masthead-Thumb-03', true);?>

    <div class="span-24 column rounded">
    <div class="span-6 column rotated">
    <a id="image1" class="cms-editable polaroid" href="#pic-1" rel="lightbox" title="first image" width="250px"><img src="<?php bloginfo ('template_url'); ?>/images/<?php echo $MastheadThumb03Path ?>" alt="image" /></a>
    </div>
    <div class="span-6 column rotated">
    <a id="image2" class="cms-editable polaroid" href="#pic-2" rel="lightbox" title="second image" width="250px"><img src="<?php bloginfo ('template_url'); ?>/images/<?php echo $MastheadThumb02Path ?>" alt="image" /></a>
    </div>
    <div class="span-6 column rotated">
    <a id="image3" class="cms-editable polaroid" href="#pic-3" rel="lightbox" title="third image" width="250px"><img src="<?php bloginfo ('template_url'); ?>/images/<?php echo $MastheadThumb01Path ?>" alt="image" /></a>
    </div>
    </div>
    <div id="pic-1">
    <img src="<?php bloginfo ('template_url'); ?>/images/<?php echo $MastheadImage03Path ?>" alt="image" />
    </div>
    <div id="pic-2">
    <img src="<?php bloginfo ('template_url'); ?>/images/<?php echo $MastheadImage02Path ?>" alt="image" />
    </div>
    <div id="pic-3">
    <img src="<?php bloginfo ('template_url'); ?>/images/<?php echo $MastheadImage01Path ?>" alt="image" />
    </div>

    <div class="clear"></div>
    </header>
  • SOLVED: I created a work around by loading a different header file for the blog page with the images in question hard coded.
  • $hardCoded === 'BOGUS!';
    What is the real solution to this issue?
  • This post looks like it's pretty old, but from what I can see, shouldn't all of the get_post_meta be inside the loop? I don't see the loop anywhere.
  • ChrisxClash, you're absolutely right. Running this outside of the Loop will only return data for the current page, not the actual Posts within the query.

    cre8tive1, where you're attempting to retrieve the post custom fields, run all of your code within…


    <?php if(have_posts()) : while(have_posts)) : the_post() ?>
    // custom code here
    // retrieve all posts values here…
    // $post will hold values for each individual post
    <?php endwhile; endif; ?>
  • @ChrisxClash & @roguerocket : get_post_meta() doesn't need to be in the loop, especially when you want the custom fields of the page (i.e. container) itself. The function itself requires a post/page ID (meaning that it doesn't assume the current post ID, if in the loop).

    If you set the WP home page to be a static page (and therefore also set a separate blog posts page), the solution is to use get_post_meta( get_option( 'page_for_posts' ) );

    Other functions that might also be useful to you to decide when to use the above:

    • is_home()
    • is_single()
    • is_page()

    All these are handy when you try to customize content in sidebars for instance:

      <?php
      if ( is_single() || is_page() ) {
          $meta = get_post_meta( get_the_ID() );
      } elseif ( is_home() ) {
          $meta = get_post_meta( get_option( 'page_for_posts' ) );
      }
      ?>