2013-11-22 148 views
0

我正在與ACFWordPress中繼器字段內的發佈對象(WordPress ACF)

我有一個自定義帖子類型,名爲Projects。在那裏用戶可以選擇通過ACF中繼器字段上傳特色圖像。

現在,在主頁上我已經給用戶的選項8 Post對象的項目柱式選擇。

我需要能夠通過這個主頁中繼器領域循環,拉出無論從每個「項目」後對象特色圖片項目名稱

ACF最近貶值了repeater_field函數,我認爲它將我從這裏扔掉。

但是,這裏就是我一直在嘗試與迄今爲止的工作:

<!-- check for repeater field --> 
<?php if(get_field('featured-projects')): ?> 

    <?php while(has_sub_field('featured-projects')): ?> 

     <!-- get project post objects --> 
     <?php $projects = get_sub_field('project'); ?> 

     <!-- without the loop below, this echo's all 8 projects ID's --> 
     <?php echo($projects->ID); ?><br /> 

     <!-- when added, only pulls the first project. And limits the echo above to the first ID --> 
     <?php $loop = new WP_Query(array( 
      'post_type' => 'projects', 
      'p' => $projects->ID 
     )); ?> 

     <?php while ($loop->have_posts()) : $loop->the_post(); ?> 

      <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 


     <?php endwhile; ?> 


    <?php endwhile; ?> 

<?php endif; ?> 

我試着註釋代碼,但如果有什麼簡化版,是有意義的,讓我知道。

回答

0

這是我該怎麼做,但我意識到它是從你的方法出發。我已經包含在代碼中的註釋說明:

<?php $featured_projects = get_field('featured-projects'); //Set $featured_projects to equal the array of projects from the home page repeater. ?> 

<!-- check for repeater field --> 
<?php if($featured_projects): ?> 

     <?php foreach($featured_projects as $featured_project) : //Loop through each featured project ?> 

      <?php $project_id = $featured_project['project']->ID; //Get the id for the current featured project ?> 

      <?php $project_title = get_the_title($project_id); //set $title to be the title of the project ?> 
      <?php project_featured_images = get_field('name-of-featured-repeater-field-here', $project_id); //get the repeater field of the featured images from the project post ?> 



      <h1 class='title'><?php echo $project_title; //print the title ?></h1> 

      <?php if($project_featured_images[0]): //check if you have a 1st image (size large) ?> 
       <img class='featured-image-one' src="<?php echo $project_featured_images[0]['name-of-the-featured-image-sub-field-here']['sizes']['large']; //print the url to the 1st image; ?>"/> 
      <?php endif; ?> 

      <?php if($project_featured_images[1]): //check if you have a 2nd image ?> 
       <img class='featured-image-two' src="<?php echo $project_featured_images[1]['name-of-the-featured-image-sub-field-here']['sizes']['large']; //print the url to the 2nd image (size large); ?>"/> 
      <?php endif; ?> 

     <?php endforeach; ?> 

<?php endif; ?> 

請務必填寫您的項目特色圖片轉發字段的名稱,而中繼器內的圖像子域的名稱。這顯然是比API版本更標準的基於PHP的解決方案。我通常根據Elliot Candon(ACF開發人員)的建議使用此方法。

您還可以通過將「大」更改爲另一個標準大小或添加custom size來獲得不同圖像大小的特色圖像。

+0

這是什麼運氣? –