我第一次使用“类型”wordpress插件。我创建了一个自定义帖子类型,以及一些自定义字段。
如何将所有自定义帖子动态调用到引导传送带中,并显示该循环中的字段?
我如何限制将循环通过传送带的帖子数量?另一个要求是,我需要将必要的引导“活动”类仅添加到第一篇文章中。
这是我的第一个镜头,(还请注意,我有一些自定义的js用于为旋转木马创建分页,但这不是一个问题(到目前为止!))
<!-- need to limit the entire carousel to displaying the 5 latest posts -->
<div id="myCarousel2" class="carousel">
<div class="carousel-inner2">
<?php $loop = new WP_Query( array( \'post_type\' => \'testimonial\' ) ); ?>
<div class="item active" data-title=""><!-- the first div needs a class of active, but the others do not -->
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="slide-copy"><?php the_content(); ?></div>
<!-- need to display a custom field or two here -->
<?php endwhile; ?>
</div>
</div>
</div><!-- end myCarousel -->
这是我的第二次尝试。除了显示自定义字段值之外,我的工作非常出色。有什么建议吗?看起来语法是正确的。。。我是不是缺少一些基本的东西?
<?php $the_query = new WP_Query(array(
\'post_type\' => \'testimonial\',
\'posts_per_page\' => 1
));
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="item active" data-title="">
<div class="slide-copy">
<?php the_content();?>
<span class="byline"><?php echo get_post_meta($post->ID, \'authorinfo\', true); ?></span>
</div>
</div>
<?php endwhile; wp_reset_postdata(); ?>
<?php $the_query = new WP_Query(array(
\'post_type\' => \'testimonial\',
\'posts_per_page\' => 5,
\'offset\' => 1
));
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="item" data-title="">
<div class="slide-copy">
<?php the_content();?>
<span class="byline"><?php echo get_post_meta($post->ID, \'authorinfo\', true); ?></span>
</div>
</div>
SO网友:Alex Lane
我发现像这样的事情,get_posts
更容易。
<?php
// Set up your arguments array to include your post type,
// order, posts per page, etc.
$args = array(
\'post_type\' => \'testimonial\',
\'posts_per_page\' => 3,
\'orderby\' => \'date\',
\'order\' => \'DESC\'
);
// Get the posts
$testimonials = get_posts($args);
// Loop through all of the results
foreach ($testimonials as $testimonial)
{
// Since we\'re doing this outside the loop,
// Build the apply the filters to the post\'s content
$content = $testimonial->post_content;
$content = str_replace(\']]>\', \']]>\', $content);
$content = apply_filters(\'the_content\', $content);
// Build out the carousel item
?>
<div class="item-active">
<?php echo get_post_thumbnail($testimonial->id); ?>
<div class="carousel-caption">
<h4><?php echo $testimonial->post_title; ?></h4>
<?php echo $content; ?>
</div>
</div>
<?php
}
?>
这已经为我工作了很多次,它已经成为我所有旋转木马、jQuery或Twitter引导的go-to方法。
我真的希望这能有所帮助。
Codex Function Reference for get_posts