0

I have some code, namely -

<?php while (has_sub_field('services_featured_links')) {

$postObjects = get_sub_field('services_link');

if($postObjects){ ?>
<ul class="intro-menu">
<?php foreach($postObjects as $post){
setup_postdata($post); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
}
wp_reset_postdata();
}
?>
</ul>
 <?php } ?>

I have 4 post objects in my repeat fields - at the moment, utilising this code returns a lot of empty li and a tags. It also returns the parent page as a link.

I basically just need to get the permalink and title of each post object I have selected.

Any help please?

Palemo
  • 217
  • 1
  • 5
  • 19
  • the_permalink & the_title works only inside a have_posts() loop. Try get_permalink() & get_the_title() – Nishant Aug 12 '14 at 09:21

1 Answers1

0

$postObjects is not what you think it is.

ACF returns the value of the field called services_link, if that's just a field then you probably have a string inside $postObjects.

If this is for a repeater block, and I'm willing to guess it is, then you need to loop through and initialise it with the_row().

I'm assuming services_featured_links is the name of your repeater, services_links is the name of the field you use for the link within that repeater, and I'm also assuming you have a services_title field. A lot of assumptions there.

My guess is you'll want something along the lines of the following:

<?php if( have_rows('services_featured_links') ){ ?>
  <ul class="intro-menu">
  <?php while (have_rows('services_featured_links')) { the_row();

    $services_link = get_sub_field('services_link');
    $services_title = get_sub_field('services_title');
    <li><a href="<?php echo $services_link; ?>"><?php echo $services_title; ?></a></li>
  <?php } ?>
  </ul>
<?php } ?>
Mike
  • 8,767
  • 8
  • 49
  • 103