admin管理员组

文章数量:1333424

My site's default archive sorting is set to alphabetical, which I am happy with. However, I want to create a custom page template so I can also have an archive page which sorts all posts by date.

I'm using a custom theme, which makes this more complicated than I expected. The standard WP help tutorials don't seem to apply here, or at least I can't figure out where.

This is where I'm at. I've copied the theme's standard archive page and given it a custom template name. However, beyond that I can't figure out where I would edit to force it to sort by date.

Can anyone help?

<?php
/**
 * Template Name: Archive (Sorted by Newest)
 * The template for a custom archive page.
 */
get_header(); ?>

<?php the_archive_title('<h1>','</h1>'); ?>

<section class="content">
     
    <?php if(have_posts()) : while(have_posts()) : the_post(); ?>
                
        <article <?php post_class('grid_block index box'); ?>>
            <?php if( get_theme_mod( 'crissy_post_link', 'block_link' ) == ('block_link')) { ?>
                
                <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"> 
                    <?php if( get_theme_mod( 'crissy_feat_img' ) == ('rectangle_no_margins')) { get_template_part('feat_img'); } ?>
                    <div class="wrapper">
                        <?php 
                            if( get_theme_mod( 'crissy_feat_img', 'circle' ) != ('rectangle_no_margins')) { get_template_part('feat_img'); }
                            get_template_part('content', get_post_format()); // content depending on post format
                            get_template_part('article_icon'); // article icon at the bottom
                         ?>
                    </div>
               </a> 
               
            <?php } else { ?>
  
                <?php if( get_theme_mod( 'crissy_feat_img' ) == ('rectangle_no_margins')) { get_template_part('feat_img'); } ?>
                <div class="wrapper">
                    <?php 
                        if( get_theme_mod( 'crissy_feat_img' ) != ('rectangle_no_margins')) { get_template_part('feat_img'); }
                        get_template_part('content', get_post_format()); // content depending on post format
                        get_template_part('article_icon'); // article icon at the bottom
                     ?>
                </div>
            <?php } ?>            
        </article>
        
    <?php endwhile; endif; ?>

</section>
<?php 
    get_template_part( 'posts_nav' );   
    get_footer(); 

?>

My site's default archive sorting is set to alphabetical, which I am happy with. However, I want to create a custom page template so I can also have an archive page which sorts all posts by date.

I'm using a custom theme, which makes this more complicated than I expected. The standard WP help tutorials don't seem to apply here, or at least I can't figure out where.

This is where I'm at. I've copied the theme's standard archive page and given it a custom template name. However, beyond that I can't figure out where I would edit to force it to sort by date.

Can anyone help?

<?php
/**
 * Template Name: Archive (Sorted by Newest)
 * The template for a custom archive page.
 */
get_header(); ?>

<?php the_archive_title('<h1>','</h1>'); ?>

<section class="content">
     
    <?php if(have_posts()) : while(have_posts()) : the_post(); ?>
                
        <article <?php post_class('grid_block index box'); ?>>
            <?php if( get_theme_mod( 'crissy_post_link', 'block_link' ) == ('block_link')) { ?>
                
                <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"> 
                    <?php if( get_theme_mod( 'crissy_feat_img' ) == ('rectangle_no_margins')) { get_template_part('feat_img'); } ?>
                    <div class="wrapper">
                        <?php 
                            if( get_theme_mod( 'crissy_feat_img', 'circle' ) != ('rectangle_no_margins')) { get_template_part('feat_img'); }
                            get_template_part('content', get_post_format()); // content depending on post format
                            get_template_part('article_icon'); // article icon at the bottom
                         ?>
                    </div>
               </a> 
               
            <?php } else { ?>
  
                <?php if( get_theme_mod( 'crissy_feat_img' ) == ('rectangle_no_margins')) { get_template_part('feat_img'); } ?>
                <div class="wrapper">
                    <?php 
                        if( get_theme_mod( 'crissy_feat_img' ) != ('rectangle_no_margins')) { get_template_part('feat_img'); }
                        get_template_part('content', get_post_format()); // content depending on post format
                        get_template_part('article_icon'); // article icon at the bottom
                     ?>
                </div>
            <?php } ?>            
        </article>
        
    <?php endwhile; endif; ?>

</section>
<?php 
    get_template_part( 'posts_nav' );   
    get_footer(); 

?>
Share Improve this question asked Jun 25, 2020 at 0:55 osarusanosarusan 1113 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I've taken the answer from here, where they are changing the order in a different way, but it should work for you. It uses the pre_get_posts hook to change the ordering for the archive page only.

add_action( 'pre_get_posts', 'my_change_sort_order');

function my_change_sort_order($query){
    if(is_archive()):
     //If you wanted it for the archive of a custom post type use: is_post_type_archive( $post_type )
       //Set the order ASC or DESC
       $query->set( 'order', 'ASC' ); // ASC = oldest first, DESC = newest first
       //Set the orderby
       $query->set( 'orderby', 'date' );
    endif;
};

Try this at the top of your new page. I haven't tested, so please reply here if any problems or it doesn't work

本文标签: Creating a custom archive template that sorts post by date