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



我有一种自定义帖子类型,称为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(); ?>

您可以在URL中添加" Year" = 2019的链接。然后根据"年"参数过滤结果:

 $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-allys-when-posts-have-have-been-publy

最新更新