donglvmang8638 2014-10-24 19:32
浏览 45
已采纳

too long

I apologize if this is answered somewhere on the site, but I've been searching since yesterday and can't seem to find an answer.

I am trying to create a grid of different sized images in a custom post type. I would need the images to be added in the editor and not anywhere else. The custom post type has a featured image at the top that I created using the_post_thumbnail(); Below that is some text, and below that I have added some images. The text and images have been added through the editor in the admin panel.

I am not sure if this is the right way to go about it, but I've been using the get_children() function to grab all the images I've uploaded to the post. This is my code for that section:

<?php
$featuredImage = get_post_thumbnail_id( $post->ID );
$attachments = get_children( array( 'post_parent'=>$post->ID, 'post_type'=> 'attachment', 'exclude'=>$featuredImage ) );

    if ( $attachments ) {
    foreach ( $attachments as $attachment ) {
        $image_attributes = wp_get_attachment_image_src( $attachment->ID, 'thumbnail' )  ? wp_get_attachment_image_src( $attachment->ID, 'thumbnail' ) : wp_get_attachment_image_src( $attachment->ID, 'full' );

        echo '<img src="' . wp_get_attachment_thumb_url( $attachment->ID ) . '" class="project-image w3">';
    }
}
?>

This displays all the images except the featured image. How can I display only the second image, for example?

In the end I would like to make it so that I have an if statement that checks for number of images added, and based on the number of images in the post, apply different css classes to different images in the get_children array and display them, replacing the same images that I have added through the editor.

I really appreciate the help. Please let me know if I haven't been clear enough, or if I'm going about this the wrong way.

Thanks!

Update

I am trying to replace the images I added through the wordpress editor with the new images in their desired divs I created using the get_children() function. Is preg_replace() the way to go?

  • 写回答

1条回答 默认 最新

  • dqys98341 2014-10-24 20:09
    关注

    The code snippet you are looking for is on the man page:

    http://codex.wordpress.org/Function_Reference/get_children#Show_the_first_image_associated_with_the_post_and_re-key_the_array

    What this does is puts all of the post IDs into an array called rekeyed_array

    Then you can access the first, second, etc by referring to the array by changing the index (0) to what you are looking for.

    $child_image = $rekeyed_array[0];  
    

    Once you have this level of control, then adding the css classes should be fairly straightforward based on the number of attachments. If you want more details on this please ask.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看