Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Unable to display custom fields value in widget WordPress

  • SOLVED

I am using a custom recent posts widget.

I am trying to modify it to display some custom fields that are defined when writing blog posts.

I have created two custom fields. "event_meta" and "event_time"

They are there in the post editor and are saving the values properly.

This is the part of the widget that I am working with



<?php echo $before_widget; ?>
<?php if ( $title ) echo $before_title . $title . $after_title; ?>
<ul>
<?php while ($r->have_posts()) : $r->the_post(); ?>
<li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?></a> <?php $key="event_time"; echo get_post_meta($post->ID, $key, true); ?>
</li>
<?php endwhile; ?>
</ul>
<?php echo $after_widget; ?>


I have

<?php $key="event_time"; echo get_post_meta($post->ID, $key, true); ?>


however when viewing the page with the widget, it is NOT displaying the time at all.

Can someone let me know what I am doing wrong?

I should add that



<?php the_meta(); ?>

works in the same spot, and does output the two custom fields I have so I know the values are being kept.

Answers (3)

2011-08-30

AdamGold answers:

Try using
get_the_ID();

instead of
$post->ID


AdamGold comments:

Sorry, my bad. Edited my answer. you should use
get_the_ID();

instead of $post->ID


Dan | gteh comments:

that worked. what is the difference?

since you both posted the solution at 3:29 I'll have to split the prize since I don't know who was first.


AdamGold comments:

Well my first answer was the_ID(), I just changed it to get_the_ID() since I remembered the_ID is echoing the result. get_the_ID is returning the result not echoing it.

2011-08-30

Ivaylo Draganov answers:

Hello,

Are the title and permalink correctly printed? Please post the full code. You have a <em>$r = new WP_Query;</em> somewhere, right?


Ivaylo Draganov comments:

Try using get_the_ID instead of $post->ID then:

get_post_meta(get_the_ID(), $key, true)


Dan | gteh comments:

Everything else is working fine (title, permalink etc..) Here is the rest of it



<?php $r = new WP_Query(array('cat' =>6, 'showposts' => $number, 'nopaging' => 0, 'post_status' => 'publish', 'caller_get_posts' => 1));
if ($r->have_posts()) :
?>
<?php echo $before_widget; ?>
<?php if ( $title ) echo $before_title . $title . $after_title; ?>
<ul>
<?php while ($r->have_posts()) : $r->the_post(); ?>
<li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?></a> <?php the_meta(); ?>
<?php echo get_post_meta($post->ID,'event_date', true) ?>

</li>
<?php endwhile; ?>
</ul>
<?php echo $after_widget; ?>


Dan | gteh comments:

get_the_ID worked

2011-08-30

Julio Potier answers:

Hello

Can you share us the full code ?

I think a "global $post;" is missing, but not sure.

Thank you