0


I am having a problem accessing my Advanced Custom Fields that I assigned to my post type.

enter image description here

To loop through the posts in my events types I did this:

<?php
   $slider = get_posts(    $slider = g array(
    'post_type' => 'event',
    'posts_per_page' => - 1
   )); ?>
<?php
   $count = 0; ?>
<?php
   foreach($slider as $slide): ?>
<?php
   $title = the_field('event_name'); ?>
<div class="item <?php
   echo ($count == 0) ? 'active' : ''; ?>">
   <img src="<?php
      echo wp_get_attachment_url(get_post_thumbnail_id($slide->ID)) ?>" class="img-responsive yooo"/>
   <div class="carousel-caption">
      <div class="caption-text">
         <hr style="width: 60%;border-bottom: 1px solid #000;margin-top: 0px;margin-bottom: 50px;">
         <img src="<?php
            bloginfo('stylesheet_directory'); ?>/assets/img/icons/note_icon.svg" onerror="this.src='<?php
            bloginfo('stylesheet_directory'); ?>/assets/img/icons/music_note.png'" alt="" class="img-responsive img--center">
         <h4><?php
            echo get_the_title($ID); ?></h4>
         <h4><?php
            the_field('event_date'); ?></h4>
         <p><?php
            the_content(); ?></p>
         <hr style="width: 60%;border-bottom: 1px solid #000;margin-bottom: 0px;margin-top: 50px;">
      </div>
   </div>
</div>
<?php
   $count++; ?>
<?php
   endforeach; ?>

I get the featured image from the post but I cant retrieve the content (text), title or my advanced custom fields.

2 Answers 2

0
<?php
   $slider = get_posts(    $slider = g array(
    'post_type' => 'event',
    'posts_per_page' => - 1
   )); ?>
<?php $count = 0; ?>
<?php foreach($slider as $slide): ?>
<?php
   setup_postdata( $slide);
   $title = the_field('event_name'); ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>

Or

<?php
   $slider = get_posts(    $slider = g array(
    'post_type' => 'event',
    'posts_per_page' => - 1
   )); ?>
<?php $count = 0; ?>
<?php foreach($slider as $slide): ?>
<?php
   $post_id = $slide->ID;
   $title = the_field('event_name',$post_id); ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
0

get_posts() and the foreach loop doesn't populate the $posts object, so things like the_content() won't be populated with the $slide content. You'll need to either use the full WP_Query method, or pass $slide->ID to each function to tell it explicitly that you're asking about that particular post.

eg: get_the_title($slide->ID) (typo in your code there, btw); get_field('event_name', $slide->ID) (use get_field() btw as the_field() prints)

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.