如何在WordPress媒体库中使用高级自定义字段插件?

时间:2016-02-09 18:46:12

标签: php wordpress advanced-custom-fields

我正在使用http://jsfiddle.net/frizzo28/pytv69fm/14/ WordPress插件为我的媒体库中的图片添加类别选择器。最终我会展示所有具有特定类别的图像。

当我从库中获取所有内容时,我想抓住所选类别并将其添加到我的the_field()数组中,但是当我调用get_field()和/或<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> 时没有回报。

此调用在WordPress帖子循环之外,因此我传递了每个附件的ID(根据Advanced Custom Fieldsthe documentation),但这似乎也不起作用。

我错过了什么吗?

的index.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>";
    }   
  } 
}

的functions.php:

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,
    ));
}  

修改

添加了字段组导出代码:

int index = new java.util.Random().nextInt(eMinor.size());
String value = eMinor.get(index);

0 个答案:

没有答案