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.

Let's say you have a few custom fields (I use the advanced custom fields plugin for it). There is an extra field in posts called 'afbeelding_onder'.

It is not required but on index.php within the loop the custom field 'afbeelding onder' from the last post will be printed. If it is possible, is there a way to check if the custom field is empty, if it isn't than it will print the value, but else (when the custom field is empty) the value of the last post which contains a value in that box will be printed.

Hope my question was clear enough, my English grammar is not that good.

share|improve this question

1 Answer 1

up vote 1 down vote accepted
if(!get_field('afbeelding_onder'))
{
    $prev_post = get_previous_post();
    echo '<p>' . get_field('afbeelding_onder', $prev_post->ID) . '</p>';
}
else
{
    echo '<p>' . get_field('afbeelding_onder') . '</p>';
}

not tested. you might want to include a while loop, if the previous post does also not have that value set?

share|improve this answer
    
Thanks, this is a nice code to study :) –  Maartje Nov 28 '13 at 12:06

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.