我有一些尝试解决的任务可能不是最聪明的方式,但到目前为止我只得到了这个想法。我有5个自定义帖子类型。在每一个 - 一个帖子。希望这些帖子循环播放并显示在我的 index.php 上。此外,这些帖子还有来自 ACF 插件的 the_field()功能。 结果不是我的预期。这是 index.php
中的代码<?php
$posttypes = array( 'plays','watch','news','shop','contact' );
$args = array(
'post_type' => $posttypes
, 'posts_per_page' => 5 );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<div id="owl-demo" class="owl-carousel owl-theme">
<?php foreach($posttypes as $posttype){?>
<?php $the_query->the_post(); ?>
<div class="item">
<?php get_template_part( 'single', $posttype ); ?>
</div>
<?php };?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</div>
此外,我会发布一个帖子的代码,例如 single_news.php
<?php get_header();
while(have_posts()):the_post();?&gt;
<div class="container-fluid "> <!-- play section -->
<div class="row">
<div class="col-xs-12 col-sm-2 col-md-2 col-lg-2">
<h2 class="" style="text-transform:uppercase"><?php the_field('name');?></h2>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-2 col-md-2 col-lg-2">
<h2 class="" style="text-transform:uppercase"><?php the_field('news_name');?></h2>
</div>
</div>
</div> <!-- row -->
</div>
实际上这个文件的结尾我不能发布,但它本身在关闭潜水和结束时是正确的。
答案 0 :(得分:0)
In args you have post_per_page set to 5 – this means it will only return the 5 latest posts, regardless of Custom Post Type (CPT).
Try '-1' (return everything) for debugging.
`$args = array(
'post_type' => $posttypes,
'posts_per_page' => -1
);`
Do you want it to return 1 latest post from each CPT?
Maybe try this...
<?php while( $the_query->have_posts() ) : ?>
<?php $the_query->the_post(); ?>
<div class="item">
<?php get_template_part( 'single', get_post_type() ); ?>
</div>
<?php endwhile; ?>
Or run query for each posttype...
<?php foreach( $posttypes as $posttype ) : ?>
<?php
$args = array(
'post_type' => $posttype,
'posts_per_page' => 5 );
$the_query = new WP_Query( $args );
?>
<?php while( $the_query->have_posts() ) : ?>
<?php $the_query->the_post(); ?>
<div class="item">
<?php get_template_part( 'single', $posttype ); ?>
</div>
<?php endwhile; ?>
<?php endforeach; ?>
//I think endforeach exists, otherwise use curly braces
Not sure exactly what you want without seeing it visually.