0

WordPressACFを使用しています。

というカスタム投稿タイプがありProjectsます。その中で、ユーザーはACF リピーター フィールドを介して2 つの主役の画像をアップロードするオプションがあります。

さて、ホームページで、 Projects 投稿タイプから8 つの投稿オブジェクトを選択するオプションをユーザーに与えました。

このホームページのリピーター フィールドをループして、各「プロジェクト」投稿オブジェクトから注目の画像とプロジェクト タイトルの両方を引き出す必要があります。

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; ?>

コードにコメントを付けようとしましたが、意味が分からない場合はお知らせください。

4

1 に答える 1

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 開発者) の推奨に従ってこの方法を使用します。

また、「大」を別の標準サイズに変更するか、カスタム サイズを追加することで、注目の画像のさまざまな画像サイズを取得することもできます。

于 2013-12-10T18:06:49.753 に答える