custom post for slider

时间:2017-09-19 作者:sanjay

我正在尝试创建自定义滑块,以便在同一个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> 

2 个回复
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();?> 

SO网友:Jacob Peattie

移动<ul> 超出while 回路:

$loop = new WP_Query( $args );
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; wp_reset_postdata();
echo \'</ul>\';

结束

相关推荐

用PHP定制WooCommerce

这是我的第一个问题,对不起,如果放错地方了。我想知道如何使用PHP学习WooCommerce编码的基础知识(类、挂钩等)。每次我有问题都要在谷歌上搜索,结果我用黑客和低效的代码来完成简单的事情。我特别想了解这一点:https://docs.woocommerce.com/wc-apidocs/index.html. 它看起来有很多信息,但不幸的是,它并不能真正解释任何事情。有人有主意吗?提前非常感谢您的帮助。