我尝试自定义自定义帖子类型并根据帖子的顺序更改输出。我在下面有以下内容,但订单输出正常工作
输出看起来像这样(它在前3个帖子后跳过):
我希望它能像这样出现:
John Doe,Vintage Burbank 1 2
<?php
$args = array(
'post_type' => 'testimonials',
'posts_per_page' => -1
);
$query = query_posts($args);
?>
<?php $i = 1; while (have_posts()) : the_post(); ?>
<?php if($i%3 == 0) : ?>
<?php the_title();?> three
<?php elseif($i%2 == 0) : ?>
<?php the_title();?> two
<?php else : ?>
<?php the_title();?>
<?php endif; ?>
<?php $i++; ?>
<?php endwhile;?>
<?php wp_reset_query(); ?>
答案 0 :(得分:0)
为什么你到处使用?此外,你需要重置$ i,当你得到一个匹配,它应该工作。试试这个:
<?php
$args = array(
'post_type' => 'testimonials',
'posts_per_page' => -1
);
$posts = get_posts($args);
$i = 1;
foreach ($posts as $post) {
setup_postdata($post);
if($i%3 == 0) {
echo get_the_title().' three';
$i = 0;
}
elseif($i%2 == 0) {
echo get_the_title().' two';
$i = 0;
}
else {
the_title();
}
$i++;
}
wp_reset_postdata();
?>
答案 1 :(得分:0)
好的,我认为您的代码没问题,唯一的问题是您正在严重更新$i
。我修改了你的代码,我认为它应该可行(如果没有,请告诉我)。
<?php
$args = array(
'post_type' => 'testimonials',
'posts_per_page' => -1
);
$query = query_posts($args);
?>
<?php $i = 1;
while (have_posts()) : the_post(); ?>
<?php if ($i % 3 == 0) : ?>
<?php the_title(); ?> three
<?php elseif ($i % 2 == 0) : ?>
<?php the_title(); ?> two
<?php else : ?>
<?php the_title(); ?>
<?php endif; ?>
<?php /* Comment this line */ ?>
<?php /* $i++; */?>
<?php /* Add this one */ ?>
<?php $i = ($i >= 3) ? 1 : ($i + 1); ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>