Forums
Home / Fixing WordPress / Unable to retrieve custom field contents via get_post_meta
(@flossiet)
14 years, 2 months ago
Following on from an earlier post about evaluating on a non-empty custom field.
Each of our longer posts has a quirky subtitle. I want to make the recent posts list in the sidebar display the quirky subtitle instead of the title.
I’ve created a function in functions.php as follows:
function show_subtitle() { $id = $post->ID; $key = 'Subtitle'; $single = 'true'; // return only the first value $subtitletext = get_post_meta($id, $key, $single); echo $subtitletext; }
Then in the sidebar, I’ve got:
<?php $recent = new WP_Query('showposts=5&offset=1'); if($recent->have_posts()) : while($recent->have_posts()) : $recent->the_post(); ?> <p><?php the_author(); ?> - <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php show_kicker(); ?></a> <?php endwhile; endif; ?></p>
This is outputting a list of five authors + link, but no text within the link.
I’m absolutely certain the posts in question have some text in the custom field I’m trying to use.
Any ideas as to what I’m missing would be greatly appreciated.
Found the answer in this post in the forums: $post->ID wasn’t sufficient, get_the_ID() did the trick.