Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

i am trying to display first post of 3 categs on my index page. so for that i wrote a little function as follows:

function excateg($categ) {
$recent = new WP_Query("cat=$categ&showposts=1");
while($recent->have_posts()) : $recent->the_post();
$imageurl = get_post_meta($post->ID, 'post-img', true);
endwhile; 
}?>

Afterwards the Html for showing the image and title and image.. but i am unable to get the custom field as above in $imageurl to get a value..

Help is appreciated!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

You need to define the $post global variable if you want to access its ID property:

function excateg($categ) {
  global $post;
  $recent = new WP_Query("cat=$categ&showposts=1");
  while($recent->have_posts()){
    $recent->the_post();
    $imageurl = get_post_meta($post->ID, 'post-img', true);
  }
}

Use it after the_post() because that function will set up that variable.

Alternatively you can use the $recent->post property instead of $post->ID...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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