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
1 Answer
Reset to default 1I'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
版权声明:本文标题:Creating a custom archive template that sorts post by date 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742320233a2452637.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论