0

I've created a custom field ("breaking") with the goal that whenever one post has this field present, it will display the value in the header throughout the site (that is, all posts, pages, archives, etc.).

The issue I'm having is that it is only displaying on the home page and on the specific post page that has the custom field, but nowhere else.

Code used (from header.php):

<?php 
global $wp_query;
$postid = $wp_query->post->ID; ?>
<?php if( get_post_meta($postid, 'breaking', true) ) { ?>
    <div>
    <a href="<?php the_permalink(); ?> "><?php echo get_post_meta($postid, 'breaking', true); ?></a>
    </div>
<?php } ?>
<?php wp_reset_query();
?>

How can I get this to display site wide?

1

To display post meta value from a specific post in header, you can print it like this with specifying post ID instead of variable $post-ID.

<?php echo get_post_meta( '1234', 'breaking', true ); ?>

get_post_meta accepts post id and if you only need to show post meta from one specific post then you can define post ID in get_post_meta and it will always show meta value from 1 post only.

So following will be your updated code. Here 1234 is the ID of your post, you should replace it with yours. Note that you will also need to add post ID in permalink too.

<?php if ( get_post_meta( '1234', 'breaking', true ) ) { ?>
    <div>
    <a href="<?php echo get_permalink( '1234' ); ?> "><?php echo get_post_meta( '1234', 'breaking', true ); ?></a>
    </div>
<?php } ?>

EDIT:

Of course it will not work throughout the website for post meta with dynamic post id variable because Archive, category, search, author, tags pages do not have any post ID. What you can do instead is, print post meta from post/page that have ID but if there is a empty result then print post meta from specific post/page ID.

<?php 
    global $wp_query;
    if ( get_post_meta( $post->ID, 'breaking', true ) ) { ?>
        <div><a href="<?php the_permalink(); ?> "><?php echo get_post_meta($postid, 'breaking', true); ?></a></div>
    <?php } else { ?>
        <div><a href="<?php echo get_permalink( '1234' ); ?> "><?php echo get_post_meta( '1234', 'breaking', true ); ?></a></div>
    <?php }
    wp_reset_query();
?>
2
  • Thanks for the reply. I need post ID to remain dynamic, as the breaking post will change regularly. I've tested it out with a static post id, and it displays post meta on all pages (desired outcome). However, when I change it back to dynamic post id, the problem persists (only displays on home page and specific post page). – egg_sanwich Oct 8 '14 at 11:19
  • Of course it will not work throughout the website for post meta with dynamic post id variable because Archive, category, search, author, tags pages do not have any post ID. Instead you can print post meta from specific post/page if there is a empty result. Check edit above. – Robert hue Oct 8 '14 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.