0

I have the following code in a template, it and displays images form a custom field:

<ul class="about-us-grid">
<li>
<?php $posts = get_posts('post_type=employee&orderby=rand&numberposts=16'); foreach($posts as $post) { ?>

<img  class="headshot" src="<?php the_field('headshot'); ?>">

    <?php } ?>
</ul>

It works in a pagetemplate.php file but when I place it into a widget, it doesn't pull the php data. I'm using PHP Code Widget. I've tried several other PHP widget plugins as well, so I don't thats the issue. Do I have to code this differently for it work in a widget versus a template?

1 Answer 1

0

Tuns out it needed: global $post

Complete working code:

<div class="aboutgridcont">
<ul class="about-us-grid">
<li>

<?php
global $post;
$posts = get_posts('post_type=employee&orderby=rand&numberposts=16'); foreach($posts as $post) { ?>

<img  class="headshot" src="<?php the_field('headshot'); ?>">

    <?php } ?>
</ul>

  </div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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