S
S
sigmasimple2016-01-27 21:36:52
JavaScript
sigmasimple, 2016-01-27 21:36:52

How to display different images on a slider?

steamchenin.com Can

you please tell me how to display different images on the slider? Multiple images have been uploaded to WP, but only the latest one by upload date is displayed.

<?php
    //Вывод записей
    $args = array(
  'post_type' => 'slider',
  'posts_per_page' => -1
);
  $slider = new WP_Query( $args );
  ?>

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

  <!-- Записи -->
  <div id="slider" class="owl-carousel owl-theme">
    <div >
    <img src="<?php $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large' ); echo $large_image_url[0]; ?>" alt="<?php the_title(); ?>">
    </div>
    
    <div>
    <img src="<?php $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large' ); echo $large_image_url[0]; ?>" alt="<?php the_title(); ?>">
    </div>

    <div>
    <img src="<?php $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large' ); echo $large_image_url[0]; ?>" alt="<?php the_title(); ?>">
    </div>

    <div>
    <img src="<?php $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large' ); echo $large_image_url[0]; ?>" alt="<?php the_title(); ?>">
    </div>

  </div>

  <?php endwhile; else: ?>

      <h3>Картинок нет</h3>

  <?php endif; ?>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry, 2016-01-27
@DP-Studio

In your code, the same picture is displayed - a thumbnail attached to the post.
link to images like this:

<img src="<?php bloginfo('template_directory'); ?>/assets/img/slider/pic1.jpg">
<img src="<?php bloginfo('template_directory'); ?>/assets/img/slider/pic2.jpg">
<img src="<?php bloginfo('template_directory'); ?>/assets/img/slider/pic3.jpg">

Upload the images to /yourtheme/assets/img/slider/

I
Igor Vorotnev, 2016-01-28
@HeadOnFire

You are using WordPress Loop incorrectly. Try like this:

<?php
// Получение записей
$args = array(
  'post_type' => 'slider',
  'posts_per_page' => -1
);
$slider = new WP_Query( $args );

// Вывод результатов
if( $slider->have_posts() ) : 
?>

  <!-- Записи -->
  <div id="slider" class="owl-carousel owl-theme">

  <?php 
  // Цикл!
  while ( $slider->have_posts() ) : $slider->the_post(); 
  ?>

    <div>
      <img src="<?php the_post_thumbnail_url( 'medium' ); ?>" alt="<?php the_title(); ?>">
    </div>

  <?php 
  // Конец цикла
  endwhile; 
  ?>

  </div>
  <!-- Записи закончились -->

<?php
// Если записи не были обнаружены
else: 
?>

  <h3>Картинок нет</h3>

<?php endif; ?>

Note : the_post_thumbnail_url() function is new as of WordPress 4.4. If you have a fresh version (and it should be fresh), then it will work fine. If it's old, you'll have to change to the old workaround via attachment_src.

S
sigmasimple, 2016-01-28
@sigmasimple

Thanks for the help and the loop structure, managed to solve it through MultiPostThumbnails

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question