WordPress.org

Ready to get started?Download WordPress

Forums

get_post_meta on homepage (12 posts)

  1. Paul Wright
    Member
    Posted 2 years ago #

    Hello,

    I've seen this question in the forums but there doesn't ever seem to be a universal solution.

    I use a meta box in my posts to insert things before/after my title. Everything works fine on single pages, but on the homepage get_post_meta doesn't return the value.

    <?php
    if ( is_single() || is_home() ) {
    	$beforetitle = get_post_meta($post->ID, 'beforetitle', true);
    	if ( $beforetitle != '' ) {
    		echo $beforetitle;
    	}
    } ?>
    
    <h1 class="entry-title"><?php the_title(); ?></h1>
    
    <?php
    if ( is_single() || is_home() ) {
    	$aftertitle = get_post_meta($post->ID, 'aftertitle', true);
    	if ( $aftertitle != '' ) {
    		echo $aftertitle;
    	}
    } ?>

    You can see the homepage: http://signyourbikeout.com and the same post as a single page: http://signyourbikeout.com/2011/08/let%E2%80%99s-move-on-from-inverted-triangle-writing/

    The triangle doesn't show up in the homepage.

    I was using if is_home, etc and those calls all returned true. I also tried reset query but it didn't seem to help

  2. Sam Scholfield
    Member
    Posted 2 years ago #

    Is your homepage a page?

    If so, try using is_page('home') instead of is_home().

  3. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    Firefox can't find the server at http://www.signyourbkeout.com.

  4. Sam Scholfield
    Member
    Posted 2 years ago #

    esmi there is an 'i' missing from the link:

    http://www.signyourbikeout.com

  5. Paul Wright
    Member
    Posted 2 years ago #

    Hello,

    Thanks for the reply, but no, I'm using the hierarchy and a home.php template. I inserted your code anyway, but to no avail.

  6. Paul Wright
    Member
    Posted 2 years ago #

    Yep, sorry, fixed the URL

  7. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    I'm using the hierarchy and a home.php template.

    Get rid of home.php. It creates nothing but trouble on most sites. Stick with index.php or a custom page template for a static front page if necessary called something like homepage.php or - even better - front-page.php.

  8. Paul Wright
    Member
    Posted 2 years ago #

    Ok, I will try front-page.php on that suggestion and switch the home to a static page.

    What's the recommended use case for home.php?

  9. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    In my experience, don't use it. It's an old hackish way of having a custom front page and it just isn't needed any longer. However, it can cause issues/confusion in some cases - just like this one, in fact - because you're relying on the servers directory index order rather than WP's template hierarchy

  10. Paul Wright
    Member
    Posted 2 years ago #

    So in order to do this correctly, I have to create a physical page for home? I guess that is what I was trying to avoid.

    I've switched home.php to front-page.php and it doesn't help, next step would be to create a real page for home and set it as the static home page in settings and select a page template in the page screen. Is that the only way? I was hoping to not have a 'real' page for home for some reason.

  11. Paul Wright
    Member
    Posted 2 years ago #

    Hmm, this goes a bit deeper I think.

    After changing to page-home.php (which correctly gets called), it still doesn't retrieve the meta boxes. And now, the meta boxes I call in my header don't work for the home either. This was working fine on home (and still does for single)

    [Code moderated as per the Forum Rules. Please use the pastebin]

    I also tried a global $post in there, but it didn't help

  12. Agrione
    Member
    Posted 2 years ago #

    Hi, I had the same problem when calling get_post_meta from home.php and I was able to fix it. I found the answer here (Katie).

    I quote:

    If you are calling get_post_meta inside the loop then you should call get_post_meta(get_the_id(), 'YOURKEY', true) instead of get_post_meta($post->ID, 'YOURKEY', true)

    Strange things happens when you call get_post_meta inside a loop. In some themes developers hack the $post at the beginning and get_post_meta stops working so this is one of the solution for those particular cases too.

Topic Closed

This topic has been closed to new replies.

About this Topic