2013-09-19 3 views
0

Я использую следующий код, чтобы вытащить все изображения, загруженные в сообщение в определенной категории. Идея заключается в том, чтобы иметь изображения заполнить ползунок в список элементов, но в настоящее время они заселение так:Невозможно заполнить изображения Wordpress в списках

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

Спасибо заранее

ответ

1

Пожалуйста, измените свой код в цикле foreach, добавьте LI в цикл foreach. Пожалуйста, проверьте ниже код, я изменил в своем коде

<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(); 
?> 
1

вы можете вставить Li тег, как показано ниже упоминания foreach($media_array as $image_src){ echo '<li><img src="'.$image_src.'" /></li>'; }

+0

Спасибо за головы! Я использовал это: echo '

  • '; – babusi

    Смежные вопросы