此代码取自another answer 但稍加修改,以从公文包帖子类型加载帖子。This shows you how to load several posts and includes them in a slider. 通过对循环进行一些修改,您可以输出第一个X柱,并使用偏移参数循环它们。
JavaScript
<!-- Include Unslider JS -->
<!-- YOU SHOULD INCLUDE WP JQUERY (ENQUEUE IT YOUR THEME) -->
<script src="//unslider.com/unslider.min.js"></script>
<!-- Set up the necessary JS for the slider -->
<script type="text/javascript">
jQuery( document ).ready(function( $ ) {
$(\'.banner\').unslider({
// Add other arguments here
speed: 500
})
});
</script>
CSS
<!-- Some basic CSS for the slider -->
<!-- Remember to add your own CSS styling -->
<style type="text/css">
.banner { position: relative; overflow: auto; }
.banner li { list-style: none; }
.banner ul li { float: left; }
</style>
PHP(查询)
<!-- Output the HTML for the slider -->
<?php
// Do the custom WP_Query first
$query = new WP_Query(
array (
\'post_type\' => \'portfolio\',
\'posts_per_page\' => \'-1\',
\'meta_key\' => \'_thumbnail_id\', // only pull posts with images
)
);
// if we have posts, display them:
if ( $query->have_posts() ) : ?>
<!-- Some basic HTML for the slider
Each slide is simply an <li>
within a surrounding <ul>
inside an encasing div -->
<div class="banner">
<ul>
<?php // While we have posts, output them as <li>\'s
while ( $query->have_posts() ) : $query->the_post(); ?>
<li style="background-image: url(<?php echo wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>);">
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
</li>
<?php endwhile; wp_reset_postdata(); ?>
</ul>
</div>
<?php else : ?>
<!-- ENTER HTML IF THERE\'S NO SLIDES HERE -->
<?php endif; ?>
现在,为了实现这一点,您可以简单地将这些代码片段粘贴到模板文件中(但我已经将它们单独包括在内,以防您想将它们包括在主题的CSS/JS文件中)。
You\'ll need to modify the Loop 包括PHP代码的前9篇文章段和CSS,以适合您的主题,但这个示例应该可以。还请确保查看完整的文档和选项列表
Unslider