2

WordPress Web サイトで Advanced Custom Fields プラグインを使用しています。子ページのリピーター フィールド ('photos_presse') の最初の画像 ('photo') をページに表示しています。これが私が使用しているphpコードです。

        <?php

            $args = array(
            'post_type'      => 'page',     
            'post_parent'    => $post->ID,
            'order'          => 'ASC',
            'orderby'        => 'menu_order',
            'post_status'   => 'publish',
            'number'        => 'no limit',
            );

            $parent = new WP_Query( $args );

            if ( $parent->have_posts() ) : ?>

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

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


            <div id="parent-<?php the_ID(); ?>" class="parent-page">

            <div class="cartouche_crop">

            <?php 
                    $first_img = true; 
                        while($first_img && has_sub_field('photos_presse')): ?> 

                     <img src="<?php the_sub_field('photo'); ?>" class="artists_menu">
                    <?php $first_img = false; ?>

            <?php endwhile; ?>


            </div>

            <h1><?php the_title(); ?></h1>
            </div>          




            </a>

            <?php endwhile; ?>



            <?php endif; wp_reset_query(); ?>

これは、最初の画像を取得するためのコードの一部です:

<?php

    $first_img = true; 
    while($first_img && has_sub_field('photos_presse')): ?> 

    <img src="<?php the_sub_field('photo'); ?>" class="artists_menu">

    <?php $first_img = false; ?>

<?php endwhile; ?>

リピーター フィールドに画像を読み込むと、サムネイル画像が作成されます。これは、WordPress 管理画面の「メディア設定」メニューで設定できます。(小中大)。

画像ごとに、小、中、大、および元のサイズの 4 つのファイルが作成されます。

私がやりたいのは、各リピーターフィールドの最初の元の画像を取得する代わりに、リピーターフィールドの最初の中サイズのサムネイルを取得したいということです。

これを行う方法が見つかりません...

誰でもこれで私を助けることができますか?

ご協力いただきありがとうございます

4

3 に答える 3

13

画像オブジェクトを返すように acf フィールドを設定する必要があります (これは [カスタム フィールド] パネルで行うことができます)。

次に、フィールドは配列を返し、次のように必要なサイズを取得できます。

$image = get_sub_field('photo');

$image_url = $image['sizes']['medium'];

または、サムネイルの場合は次のようになります。

$image = get_sub_field('photo');

$image_url = $image['sizes']['thumbnail'];

基本的に、ワードプレスが作成するすべてのサイズは、より大きな画像配列のサイズ配列にあります。


コードを統合するために OP によって要求された編集

<?php

    $first_img = true; 
    while($first_img && has_sub_field('photos_presse')):

    $image = get_sub_field('photo');

    $image_url = $image['sizes']['medium'];
?> 

    <img src="<?php echo $image_url; ?>" class="artists_menu">

    <?php $first_img = false; ?>

<?php endwhile; ?>
于 2014-02-03T12:21:47.000 に答える
0

それは簡単でシンプルです。まず、画像の戻り値を「画像オブジェクト」として設定してから、このコードを使用します

<?php $image = get_sub_field('NAMEOFYOURSUBIMAGEFIELD'); ?>
<?php if( $image ): ?>
    <img src="<?php echo $image['sizes']['YOURCUSTOMSIZE']; ?>" width="<?php echo $image['sizes']['YOURCUSTOMSIZE-width']; ?>" height="<?php echo $image['sizes']['YOURCUSTOMSIZE-height']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
于 2015-12-12T18:40:57.563 に答える
0

最初に「戻り値」を ACF 管理の画像オブジェクトに変更します [最初にオプションを更新する必要がある ACF 管理サンプルhttp://i.imgur.com/Uh1soEx.png ]

戻り値を更新した後、以下のようにコードを更新する必要があります

<?php 
$first_img = true; 
while($first_img && has_sub_field('photos_presse')):
    $image =  get_sub_field('photo');
    $image_thumb = $image['sizes']['thumbnail'] // you can add any image size available in the wordpress admin, if you want to define new image size  refer this documentation http://codex.wordpress.org/Function_Reference/add_image_size  
    echo '<img src="'.$image_thumb.'" class="artists_menu">'; 
    $first_img = false; 
endwhile; 
?>
于 2014-02-03T13:07:28.590 に答える