我正在尝试创建自定义滑块,以便在同一个li中打印所有ul。但问题是,我得到的数据来自不同的ul。这里是查询。
<?php
$args = array(
\'post_type\' => \'slider\',
\'posts_per_page\' => 5
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo \'<ul>\';?>
<li data-transition="fade" data-slotamount="6">
<?php
echo wp_get_attachment_image( get_post_thumbnail_id(), \'full\', false, array( \'class\' => \'thumbnail zoom\' ) );?>
<p><a href="#."><?php the_title();?></a></p>
</li>
<?php echo \'</ul>\';
endwhile; wp_reset_postdata();?>
这是输出
<ul> <li data-transition="fade" data-slotamount="6">
<img width="1364" height="612" src="http://localhost/wordpress/wp-content/uploads/2017/09/main-banner5.jpg" class="thumbnail zoom" alt="" srcset="http://localhost/wordpress/wp-content/uploads/2017/09/main-banner5.jpg 1364w, http://localhost/wordpress/wp-content/uploads/2017/09/main-banner5-300x135.jpg 300w, http://localhost/wordpress/wp-content/uploads/2017/09/main-banner5-768x345.jpg 768w, http://localhost/wordpress/wp-content/uploads/2017/09/main-banner5-1024x459.jpg 1024w" sizes="(max-width: 1364px) 100vw, 1364px" /> <p><a href="#.">slider2</a></p>
</li>
</ul><ul> <li data-transition="fade" data-slotamount="6">
<img width="1364" height="612" src="http://localhost/wordpress/wp-content/uploads/2017/09/main-banner4.jpg" class="thumbnail zoom" alt="" srcset="http://localhost/wordpress/wp-content/uploads/2017/09/main-banner4.jpg 1364w, http://localhost/wordpress/wp-content/uploads/2017/09/main-banner4-300x135.jpg 300w, http://localhost/wordpress/wp-content/uploads/2017/09/main-banner4-768x345.jpg 768w, http://localhost/wordpress/wp-content/uploads/2017/09/main-banner4-1024x459.jpg 1024w" sizes="(max-width: 1364px) 100vw, 1364px" /> <p><a href="#.">slider1</a></p>
</li>
</ul>
SO网友:Wh0CaREs
问题是因为内环有UL
echo \'<ul>\';
while ( $loop->have_posts() ) : $loop->the_post();
?>
<li data-transition="fade" data-slotamount="6">
<?php
echo wp_get_attachment_image( get_post_thumbnail_id(), \'full\', false, array( \'class\' => \'thumbnail zoom\' ) );?>
<p><a href="#."><?php the_title();?></a></p>
</li>
<?php
endwhile;echo \'</ul>\';
在回显UL标签之前,只需做一个IF标记
$loop = new WP_Query( $args );
if($loop-have_posts()):
echo \'<ul>\';
while ( $loop->have_posts() ) : $loop->the_post();
?>
<li data-transition="fade" data-slotamount="6">
<?php
echo wp_get_attachment_image( get_post_thumbnail_id(), \'full\', false, array( \'class\' => \'thumbnail zoom\' ) );?>
<p><a href="#."><?php the_title();?></a></p>
</li>
<?php
endwhile; echo \'</ul>\';endif; wp_reset_postdata();?>