Custom fields won’t display on my blog page

Try using:

<?php
// Determine context
$page_id = ( 'page' == get_option( 'show_on_front' ) ? get_option( 'page_for_posts' ) : get_the_ID );
// Echo post meta for $page_id
echo get_post_meta( $page_id, 'Page Description', true ); 
?>

What’s happening:

  1. You’re using a static page as front page, and a static page to display the blog posts index
  2. You’ve added your post custom metadata (i.e. custom field) to a Static Page, and then assigned that page to display the blog posts index
  3. In that scenario, WordPress uses either home.php or index.php (in that order, per the Template Hierarchy), to render the blog posts index. WordPress will never use page.php or any custom page template to display the blog posts index.
  4. In either home.php or index.php, WordPress makes no reference to ID of the page assigned to display the blog posts index.
  5. Thus, if you want to reference data associated with that page ID, you need to query it directly
  6. Enter get_option( 'page_for_posts' ), which returns the ID of the page assigned to display the blog posts index.

Note: since you’ve put this code in your header.php, it’s going to output in every template file; so you’ll want to add some fail-safes.

Edit

Example fail-safes:

  1. To display just on the blog posts index, wrap the whole thing in:

    if ( is_home() ) {}
    
  2. To display on static pages and the blog posts index, wrap the whole thing in

    if ( is_home() || is_page() ) {}
    

Leave a Comment