2

I'm using the Advanced Custom Fields WordPress plugin to add a category selector to images in my media library. Eventually I would to show all the images that have a specific category.

When I get everything from the library, I'd like to grab the selected category and add it to my $image array, but when I call the_field() and/or get_field() it returns nothing.

This call is outside the WordPress post loop, so I passed each attachment's ID (as per the documentation and this question), but that does not appear to work either.

Am I missing something?

index.php:

<div id="content">
  <div id="inner-content" class="wrap cf">
    <main id="main" class="cf" role="main" >

        <?php echo get_images_from_media_library(); ?>

    </main>
  </div>
</div>

functions.php:

function get_images_from_media_library() {
  $args = array(
    'post_type' => 'attachment',
    'post_mime_type' =>'image',
    'post_status' => 'inherit',
    'posts_per_page' => 100,
    'orderby' => 'rand',
    'post_parent' => $post->ID
  );

  $attachments = get_posts($args);

  $images = array();

  if ($attachments) {
    foreach ($attachments as $attachment) {
      $image = array(
        'id' => $attachment->ID,
        'src' => $attachment->guid,
        'title' => $attachment->post_title,
        'tags' => wp_get_post_tags($attachment->ID)
      );

      array_push($images, $image);

      // For debug purposes
      // Always returns 'FAILED'...
      if ( get_field('category_select', $attachment->ID) ) {
        $category = get_field('category_select', $attachment->ID);
        echo '<h1>Category: '.$category.'</h1>';
      } else {
        echo '<h1>FAILED</h1>';
      }

      //TODO: Add $category to $images.

      echo "<div class='gallery'>";
        echo '<img src="'.$image['src'].'" class="col span-lg-1-md-2-sm-5 / gallery__image / '.$image['title'].'">';
      echo "</div>";
    }   
  } 
}

EDIT:

Added the field group export code:

if(function_exists("register_field_group"))
{
    register_field_group(array (
        'id' => 'acf_media-category',
        'title' => 'Media Category',
        'fields' => array (
            array (
                'key' => 'field_56aa8c5f67f98',
                'label' => 'Category',
                'name' => 'category_select',
                'type' => 'radio',
                'required' => 1,
                'choices' => array (
                    'icon' => 'Icon',
                    'logo' => 'Logo',
                    'cover' => 'Cover',
                ),
                'other_choice' => 0,
                'save_other_choice' => 0,
                'default_value' => 'icon : Icon',
                'layout' => 'vertical',
            ),
        ),
        'location' => array (
            array (
                array (
                    'param' => 'ef_media',
                    'operator' => '==',
                    'value' => 'all',
                    'order_no' => 0,
                    'group_no' => 0,
                ),
            ),
        ),
        'options' => array (
            'position' => 'normal',
            'layout' => 'no_box',
            'hide_on_screen' => array (
            ),
        ),
        'menu_order' => 0,
    ));
}  
3
  • You need to add global $post; to the top of your get_images_from_media_libarary() function. You are referencing the $post variable, but it is not available inside the function until you global it in. Commented Feb 9, 2016 at 19:24
  • @KirkBeard Hmm... I'm using a custom theme, perhaps that's the culprit. I'll play around with that. Thanks, I appreciate your help. Commented Feb 9, 2016 at 21:22
  • @KirkBeard Added 'global $post' to the top of get_images_from_media_library() and removed the echo. It's displaying my gallery, but get_field('category_select', $attachment->ID) still returns nothing. I'm using WP 4.4.2, ACF 4.4.5, and a modified Bones theme (link[/link]. If the issue isn't with the above code, is there anywhere else I should look in my theme or WP configuration? Commented Feb 9, 2016 at 21:48

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.