按发布的年份过滤自定义帖子类型循环

问题描述 投票:0回答:1

我有一个名为eguide_landing的自定义帖子类型。我为此创建了一个自定义归档页面,在此循环CPT并显示它们,分成几年。这显示得很好。

我现在想在侧边栏上创建一个小部件,将所有年份显示为超链接,点击后,它会重新加载显示该年度CPT帖子的页面。因此,如果我点击2018,它将仅显示2018年的所有CPT帖子。我似乎无法弄清楚如何创建多年来的代码,将它们显示为链接,然后点击显示存档页面设计,但仅限于那一年。

这是存档页面的代码:

<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$loop = new WP_Query( array( 'post_type' => 'eguide_landing', 'paged' => $paged, 'posts_per_page' => 12 ) );


if ( $loop->have_posts() ) :
    //Display the years as headers

    $current_year = get_the_time('Y');
    echo '<h2 class="current-year">'.$current_year.'</h2>';
    while ( $loop->have_posts() ) : $loop->the_post(); 

    $this_year = get_the_time('Y');
    if( $this_year!=$current_year ):
    $current_year = $this_year;
    echo '<h2 class="current-year">'.$current_year.'</h2>';
endif;?>  

    <a href="<?php the_permalink();?>">
        <div class="eguide-single">
            <div class="left-columns">
                <div class="eguide-featured" style="background-image:url('<?php the_post_thumbnail_url();?>');"></div>
            </div>
            <div class="right-columns">
                <h2 style="margin-bottom:0;"><?php the_title();?></h2>
                <small><?php the_date();?></small>
                <?php the_excerpt();?>
            </div>
        </div>
    </a>
    <?php endwhile;

// Render the Avada pagination

endif;

$pagination = fusion_pagination( $loop->max_num_pages, $loop );
echo $pagination;

wp_reset_postdata(); ?>
php wordpress loops while-loop custom-post-type
1个回答
0
投票

您可以创建将?year = 2019添加到网址的链接。然后根据'year'参数过滤结果:

 $loop = new WP_Query( array( 'post_type' => 'eguide_landing', 'paged' => $paged, 'posts_per_page' => 12, 'year' => $_GET["year"]') );

对于年份列表,请尝试以下答案之一:https://wordpress.stackexchange.com/questions/145148/get-list-of-years-when-posts-have-been-published

© www.soinside.com 2019 - 2024. All rights reserved.