• Paul Wright

    (@signyourbikeout)


    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: https://signyourbikeout.com and the same post as a single page: https://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

Viewing 11 replies - 1 through 11 (of 11 total)
  • Is your homepage a page?

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

    Firefox can’t find the server at https://www.signyourbkeout.com.

    esmi there is an ‘i’ missing from the link:

    https://www.signyourbikeout.com

    Thread Starter Paul Wright

    (@signyourbikeout)

    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.

    Thread Starter Paul Wright

    (@signyourbikeout)

    Yep, sorry, fixed the URL

    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.

    Thread Starter Paul Wright

    (@signyourbikeout)

    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?

    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

    Thread Starter Paul Wright

    (@signyourbikeout)

    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.

    Thread Starter Paul Wright

    (@signyourbikeout)

    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

    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.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘get_post_meta on homepage’ is closed to new replies.