我在 wordpress 中有一个日历自定义帖子类型。我让它在我的模板中运行良好,但我知道有更好的方法来运行循环。我使用了当月(1 月、2 月、3 月等)的自定义分类法,并在页面上逐月对帖子进行排序。目前,这意味着我必须运行 12 个单独的循环来获得我想要的功能,我确信这不是正确的方法。
这是我正在使用的代码,任何帮助将不胜感激。谢谢!
<div class="module">
<h2>January</h2>
<?php // Annual Events Posts
$calendar = new WP_Query('post_type=calendar&month=January');
while($calendar->have_posts()) : $calendar->the_post();
?>
<div class="grid" style="background:none">
<div class="col-1">
<h3>
<?php
the_title();
?>
</h3>
<h4>
<?php
$dateValue = get_post_meta( $post->ID, 'rhc_when', true );
if($dateValue != '') {
echo $dateValue;
};
$timeValue = get_post_meta( $post->ID, 'rhc_time', true );
if($timeValue != '') { ?>, <?php
echo $timeValue;
}
?>
</h4>
<?php $content = the_content();
if( $content !='') {
echo $content;
};
?>
</div>
</div>
<?php
endwhile;
wp_reset_postdata();
?>
<hr>
<h2>February</h2>
<?php // Annual Events Posts
$calendar = new WP_Query('post_type=calendar&month=February');
while($calendar->have_posts()) : $calendar->the_post();
?>
<div class="grid" style="background:none">
<div class="col-1">
<h3>
<?php
the_title();
?>
</h3>
<h4>
<?php
$dateValue = get_post_meta( $post->ID, 'rhc_when', true );
if($dateValue != '') {
echo $dateValue;
};
$timeValue = get_post_meta( $post->ID, 'rhc_time', true );
if($timeValue != '') { ?>, <?php
echo $timeValue;
}
?>
</h4>
<?php $content = the_content();
if( $content !='') {
echo $content;
};
?>
</div>
</div>
<?php
endwhile;
wp_reset_postdata();
?>