Grow your CSS skills. Land your dream job.

Need alternative to get_post_meta for custom fields on Blog page

  • # January 10, 2011 at 6:47 am

    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

    >

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









    < ?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);?>



    image


    image


    image



    image


    image


    image




    # January 23, 2011 at 7:43 pm

    SOLVED: I created a work around by loading a different header file for the blog page with the images in question hard coded.

    # June 28, 2012 at 12:50 pm

    $hardCoded === ‘BOGUS!';
    What is the real solution to this issue?

    # June 28, 2012 at 3:00 pm

    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.

    # June 29, 2012 at 2:54 pm

    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; ?>
    # September 16, 2012 at 10:18 am

    @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’ ) );
    }
    ?>

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".