Wordpress post carousel

时间:2014-03-20 作者:Casper

我坚持这样做。我想有一个公文包旋转木马,显示旋转木马中的9个最新帖子,当你点击箭头时,你会看到9个旧帖子等等。

我怎样才能让它工作?

我的主题代码:

<section id="portfolio">
    <div class="container_12">

        <ul class="entrybox">
            <?php 
                $args = array(\'post_type\' => \'portfolio\', \'posts_per_page\' => -1, \'paged\' => $paged);
                $loop = new WP_Query($args);
                $count = 0;
            ?>

            <?php if ($loop) : while ($loop->have_posts()) : $loop->the_post(); ?>
            <li class="grid_4 portfolio-post">
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                <header class="post-thumb">
                    <?php the_post_thumbnail(\'thumbnail-portfolio\'); ?>
                </header><!-- End header.post-thumb -->

                <aside>
                    <h2><?php the_title(); ?></h2>
                    <p><?php the_excerpt(); ?></p>
                </aside><!-- End aside -->
            </a>
            </li><!-- End li.grid_4 portfolio-post -->
            <?php endwhile; ?>
            <?php else : ?>
                <p>No portfolio items were found! I\'m not sure what you\'re looking for.</p>
            <?php endif; ?>
            <?php wp_reset_query(); ?>
        </ul><!-- End ul.entrybox -->

    <div class="clear"></div><!-- End div.clear -->
    </div><!-- End div.container_12 -->
</section><!-- End section#portfolio -->

<hr>

<?php get_template_part( \'contact\' ); ?>

1 个回复
SO网友:MBL

此代码取自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

结束

相关推荐

Wrap posts p tags in div

我正在尝试设置博客帖子,以便在添加图像时,将其包装在一个div集中以向右浮动,在添加文本时,将其包装在另一个div集中以向左浮动。因此,所有添加到日志中的图像都将始终具有右侧图像和左侧文本。尝试使用jQuery将所有p标记封装在一个div中,如下所示jQuery(\'p\').wrap(\"<div class=\'post-txt\'></div>\");但我无法确定要将其添加到哪个PHP文件,所以似乎什么都没有发生。如果您能帮助我在wordpress或任何其他选项中找到正确的P