问题描述
我正在尝试在其各自的术语标题下显示每个帖子,并在其下方显示的所有术语帖子显示它们发布的月份和年份(按发布日期)。
我以为我有一些工作,但在测试过程中注意到,如果一个帖子有相同的“月份”,它会抹去同一任期内其他帖子的年份和月份以及它下面的条款,我有一些图像显示我的意思和我目前使用的代码。
在显示的图像中,为了清晰起见,我将发布日期附加到文件名后,通常只是文件名。
在发布日期月份不同时工作
我该如何解决这个问题?我是否需要使用单独的查询获取所有发布日期并保存?
感谢任何示例
/*
* Custom Template: Archive Newsletters
*/
// Remove stuff
remove_action( 'genesis_loop','genesis_do_loop' );
// Add our custom loop
add_action( 'genesis_loop','newsletter_archive_loop' );
function newsletter_archive_loop() {
//fetch the terms for the newsletter-groups taxonomy
$terms = get_terms( 'newsletter-groups',array (
'hide_empty' => 'true','exclude' => 67,//excluding parent term ID 'school-newsletters' - only want to show term children of 'school-newsletters'
));
// run a query for each term
foreach( $terms as $term ) {
// Define the query
$args = array(
'post_type' => 'newsletters','newsletter-groups' => $term->slug,'posts_per_page' => -1,);
// run the query
$query = new WP_Query( $args );
if( $query->have_posts() ) {
echo '<div class="letters">';
// output the term name in a heading tag
echo'<h4 class="term-heading">' . $term->name . '</h4>';
while ( $query->have_posts() ) { $query->the_post();
// get current month
$current_month = get_the_date('F');
// get attachements from custom field
$attachment_id = get_field('newsletter_upload');
$url = wp_get_attachment_url( $attachment_id );
$title = get_the_title( $attachment_id );
// get the filesize
$filesize = filesize( get_attached_file( $attachment_id ) );
$filesize = size_format($filesize,2);
if( $query->current_post === 0 ) {
echo '<h4 class="date">';
the_date( 'F Y' );
echo '</h4>';
} else {
$f = $query->current_post - 1;
$old_date = MysqL2date( 'F',$query->posts[$f]->post_date );
if($current_month != $old_date) {
echo '<h4 class="date">';
the_date( 'F Y' );;
echo '</h4>';
}
} ?>
<li class="letters-file">
<a href="<?PHP echo $url; ?>" title="<?PHP echo $title; ?>" target="_blank" rel="noopener noreferrer"><?PHP the_title(); ?></a><span class="letters-file-sz"> (<?PHP echo $filesize; ?>)</span>
</li>
<?PHP } // endwhile have posts
echo '</div>'; // close letters div
// use reset postdata to restore orginal query
wp_reset_postdata();
} // end if query have posts
} // end for each
} // end function
genesis(); // call genesis framework
删除 if 和 else 两个条件并尝试只打印 echo ''; the_date('F Y');回声''
它只会显示单个发布日期 - 但不会分组
预期输出
解决方法
您可以将每个月推入一个数组并检查该月份是否已经在该数组中,然后不显示重复月份。
/*
* Custom Template: Archive Newsletters
*/
// Remove stuff
remove_action( 'genesis_loop','genesis_do_loop' );
// Add our custom loop
add_action( 'genesis_loop','newsletter_archive_loop' );
function newsletter_archive_loop() {
//fetch the terms for the newsletter-groups taxonomy
$terms = get_terms( 'newsletter-groups',array (
'hide_empty' => 'true','exclude' => 67,//excluding parent term ID 'school-newsletters' - only want to show term children of 'school-newsletters'
));
// run a query for each term
foreach( $terms as $term ) {
$dates = array();
// Define the query
$args = array(
'post_type' => 'newsletters','newsletter-groups' => $term->slug,'posts_per_page' => -1,);
// run the query
$query = new WP_Query( $args );
if( $query->have_posts() ) {
echo '<div class="letters">';
// output the term name in a heading tag
echo'<h4 class="term-heading">' . $term->name . '</h4>';
while ( $query->have_posts() ) { $query->the_post();
// get current month
$current_month = get_the_date('F');
// get attachements from custom field
$attachment_id = get_field('newsletter_upload');
$url = wp_get_attachment_url( $attachment_id );
$title = get_the_title( $attachment_id );
// get the filesize
$filesize = filesize( get_attached_file( $attachment_id ) );
$filesize = size_format($filesize,2);
if( !in_array(get_the_date( 'F Y' ),$dates ) ){
$dates[] = get_the_date( 'F Y' );
echo '<h4 class="date">';
echo get_the_date( 'F Y' );
echo '</h4>';
}
?>
<li class="letters-file">
<a href="<?php echo $url; ?>" title="<?php echo $title; ?>" target="_blank" rel="noopener noreferrer"><?php the_title(); ?></a><span class="letters-file-sz"> (<?php echo $filesize; ?>)</span>
</li>
<?php } // endwhile have posts
echo '</div>'; // close letters div
// use reset postdata to restore orginal query
wp_reset_postdata();
} // end if query have posts
} // end for each
} // end function
genesis(); // call genesis framework