0

我正在使用以下代码将所有上传到特定类别帖子的图像提取出来。这个想法是让图像作为列表元素填充滑块,但目前它们填充如下:

<li>
<img...>
<img...>
<img...>
<img...>
</li>

代替:

<li>
<img...>
</li>
<li>
<img...>
</li>
<li>
<img...>
</li>

这是我正在使用的代码:

    <?php
    /*
    Template Name: template_home
    */
    get_header();
    ?>

    <script src="<?php echo get_template_directory_uri(); ?>/js/jquery.bxslider.js"></script>
    <ul class="bxslider" id="home-slider">

    <?php
    /*
    this gets all the posts from a specific category
    and all the attached images (uploaded to that post)
    it will add in the "featured" image as the first image
    */
    $category_name = 'home_slider';
    query_posts('category_name='.$category_name.'&order=ASC&showposts=-1');
    while (have_posts()) : the_post();

        $media_array = array();

        // GET FEATURED IMAGE /////////////////////////
        if (has_post_thumbnail( $post->ID ) ){
            $img_src_array = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
            $img_src = $img_src_array[0];
            $featured_img = $img_src;
            array_push($media_array, $img_src);

        }

        //get other images ///////////////////////////
        $args = array(
            'order'          => 'ASC',
            'orderby'        => 'menu_order',
            'post_type'      => 'attachment',
            'post_parent'    => $post->ID,
            'post_mime_type' => 'image',
            'post_status'    => null,
            'numberposts'    => -1,
        );
        $attachments = get_posts($args);
        if ($attachments) {
            foreach ($attachments as $attachment) {
                $img_array = wp_get_attachment_image_src( $attachment->ID, 'full');
                $img_src = $img_array[0];
                if($featured_img != $img_src){
                    array_push($media_array, $img_src);
                }
            }
        }

        //START OF PROJECT ///////////////////////////
        //displays all images ///////////////////////
        foreach($media_array as $image_src){
           echo '<img src="'.$image_src.'" />';
        }

    /*
    reset the query after we are done
    */
    ?>

             <li><img src="<?php echo $img_src; ?>" alt="home-slide"></li>
             <?php
            endwhile;
            ?>
     </ul>


      <?php
    get_footer();
    ?>

先感谢您

4

2 回答 2

1

您可以插入 li 标签,如下所述 foreach($media_array as $image_src){ echo '<li><img src="'.$image_src.'" /></li>'; }

于 2013-09-19T13:04:44.670 回答
1

请在 foreach 循环中更改您的代码,在 foreach 循环中添加 LI。请检查以下代码,我已更改您的代码

<script src="<?php echo get_template_directory_uri(); ?>/js/jquery.bxslider.js"></script>
<ul class="bxslider" id="home-slider">

<?php
/*
this gets all the posts from a specific category
and all the attached images (uploaded to that post)
it will add in the "featured" image as the first image
*/
$category_name = 'home_slider';
query_posts('category_name='.$category_name.'&order=ASC&showposts=-1');
while (have_posts()) : the_post();

    $media_array = array();

    // GET FEATURED IMAGE /////////////////////////
    if (has_post_thumbnail( $post->ID ) ){
        $img_src_array = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
        $img_src = $img_src_array[0];
        $featured_img = $img_src;
        array_push($media_array, $img_src);

    }

    //get other images ///////////////////////////
    $args = array(
        'order'          => 'ASC',
        'orderby'        => 'menu_order',
        'post_type'      => 'attachment',
        'post_parent'    => $post->ID,
        'post_mime_type' => 'image',
        'post_status'    => null,
        'numberposts'    => -1,
    );
    $attachments = get_posts($args);
    if ($attachments) {
        foreach ($attachments as $attachment) {
            $img_array = wp_get_attachment_image_src( $attachment->ID, 'full');
            $img_src = $img_array[0];
            if($featured_img != $img_src){
                array_push($media_array, $img_src);
            }
        }
    }

    //START OF PROJECT ///////////////////////////
    //displays all images ///////////////////////
    foreach($media_array as $image_src){
       echo '<li><img src="'.$image_src.'" /></li>';
    }

/*
reset the query after we are done
*/
?>

         <li><img src="<?php echo $img_src; ?>" alt="home-slide"></li>
         <?php
        endwhile;
        ?>
 </ul>


  <?php
get_footer();
?>
于 2013-09-19T13:09:45.983 回答