0

カスタムフィールド(Wordpress)から画像を取得し、1行に3つの画像を表示したいと思います。さらに、私は追加したいと思います

    行ごとのタグ。したがって、次のようになります。

    <ul> <li>image1</li> <li>image2</li> <li>image3</li> </ul>

    <ul> <li>image4</li> <li>image5</li> <li>image6</li> </ul>

    私はいくつかのコードを見つけ、これを達成するためにいくつかのカスタムコーディングを試しました。</ul>ほとんど動作しましたが、最後に取得した画像の後にタグが必要です。以下のコードは</ul>、最後に取得された画像の直前にタグを生成します...さらに、私は配列とループにあまり精通していないため、以下のコードよりも簡単にこれを実現できる可能性があります。

    誰が私を助けることができますか?何年もありがとうございました!

    <?php 
    
    // vars
    $images = array();
    $row = 0;
    $i = 0;
    $gallery_images = get_field('galerie' );
    
    
    // loop through gallery images and sort into the $images array
    if( $gallery_images )
    {
        foreach( $gallery_images as $image )
        {
    
            // Insert the url tag
            if ( $i === 0 ) 
            {
                     echo "<ul class='galerieul'>";
            }
    
            // increase $i
            $i++;
     // Insert the url tag
            if( $i > 3 )
            {
                echo "<ul class='galerieul'>";
            }
            // if $i has increased above 3, increase the row and reset $i
            if( $i > 3 )
            {    
            $i = 1;
            $row++;
            }
    
    
        // add image to row
            $images[ $row ][] = $image;
     ?>
       <li class="customimagestyle">
                        <?php echo $image['title']; ?><br />
                        <a href="<?php echo $image['url']; ?>" rel="lightbox"><img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" /></a><br />
                        <?php echo $image['description']; ?>
                    </li>
    
                    <?php
    // Insert the url tag
    
             if ( $i === 3 ) 
            {
                 echo "</ul>";
            }
            if ($i < $row) 
            { 
                echo '</ul>'; 
            }
    
        }
    }
    
    ?>
    
    4

    2 に答える 2

    0

    get_field('galerie' )動作していますか?ギャラリーで画像を取得できますか?

    画像 (添付ファイル) は投稿として保存されることに注意してください。したがって、ギャラリーを含む投稿の ID がわかっている場合、これは問題になりません。

    以下は、役立つ可能性のあるコード/ソリューションです (テストされていません)。

    function display_images_in_list($size = thumbnail) {
    
        if($images = get_posts(
            array(
            'post_parent'    => get_the_ID(),
            'post_type'      => 'attachment',
            'numberposts'    => -1, // show all
            'post_status'    => null,
            'post_mime_type' => 'image',
                'orderby'        => 'menu_order',
                'order'           => 'ASC')
            )
        ){
            foreach($images as $image) {
                $attimg   = wp_get_attachment_image($image->ID,$size);
                echo $attimg;
            }
        }
    }
    

    ソース: http://wordpress.org/support/topic/list-all-images-attached-to-a-post-based-on-gallery

    function aldenta_get_images($size = 'thumbnail') {
      global $post;
    
      $photos = get_children( 
        array(
          'post_parent' => $post->ID, 
          'post_status' => 'inherit', 
          'post_type' => 'attachment', 
          'post_mime_type' => 'image', 
          'order' => 'ASC', 
          'orderby' => 
          'menu_order ID') 
      );
    
      $results = array();
    
      if ($photos) {
        foreach ($photos as $photo) {
        // get the correct image html for the selected size
        $results[] = wp_get_attachment_image($photo->ID, $size);
      }
    
      return $results;
    }
    

    ソース: http://snipplr.com/view/52857/

    于 2013-02-20T21:10:04.007 に答える