WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Unable to retrieve custom field contents via get_post_meta (2 posts)

  1. FlossieT
    Member
    Posted 4 years 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.

  2. FlossieT
    Member
    Posted 4 years ago #

    Found the answer in this post in the forums: $post->ID wasn't sufficient, get_the_ID() did the trick.

Topic Closed

This topic has been closed to new replies.

About this Topic