[英]WordPress control both post order and number of posts in category.php
[英]How to display all posts in category.php sorted in descendent order by the date the post was added
我有一個帶有代碼bewlo的category.php文件。 問題在於,向下滾動頁面時,顯示的不是最新的帖子,而是隨機顯示的,而不是它們的完整列表! 也許有人對可能出什么問題有一些想法?
<?php get_header(); ?>
<div class="content">
<?php if ( have_posts() ) { ?>
<div class="container">
<h1>News</h1>
<div class="news">
<?php while ( have_posts() ) {
the_post();
if (has_post_thumbnail()) $thumbnail = '<div class="news_thumb">'.get_the_post_thumbnail($post->ID, 'medium').'</div>';
else $thumbnail = '';
add_filter( 'excerpt_length', 'custom_excerpt_length_author', 999 );
$content = get_the_excerpt();
remove_filter( 'excerpt_length', 'custom_excerpt_length_author', 999 );
$len = (integer) mb_strlen(get_the_content(), 'UTF-8');
if($post->post_content) $need_more = true;
else $need_more = false;
?>
<div class="news__item <?php echo ($need_more)?'news__item_more':''; ?>">
<div class="news__item-inner">
<?php echo $thumbnail; ?>
<span class="news__date"><?php the_time('d F Y'); ?></span>
<span class="news__title"><?php echo ($need_more)?'<a href="'.get_permalink().'">'.get_the_title().'</a>':get_the_title(); ?></span>
<div><?php echo $content; ?></div>
</div>
</div>
<?php }; ?>
</div>
<?php
$big = 999999999;
$pages = paginate_links(array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%',
'prev_next' => true,
'current' => max(1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'type' => 'list'
));
if($pages){
print '<div class="paging">'.$pages.'</div>';
}
?>
<div class="loader"><img src="<?php bloginfo( 'template_url' ); ?>/img/loader.gif" alt=""></div>
</div>
<?php }; ?>
</div>
<?php get_footer(); ?>
我建議研究Wordpress Codex ,特別是WP_Query類。
就您而言,您需要將循環更改為:
$args = array(
'orderby' => 'date', // Order posts by date added
'order' => 'DESC', // Specify how to order - DESC for descending, ASC for ascending
'posts_per_page' => -1 // To display all posts you need to give the value -1
);
$query = new WP_Query( $args );
<?php if ( $query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<!-- your post content (title, thumbnail, excerpt, etc.) -->
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
我認為您可以像這樣使用pre_get_posts
鈎子,添加到您的functions.php中
add_action('pre_get_posts', 'change_category_order', 10, 1);
function change_category_order( $query ){
if( $query->is_category()){
$query->set('order', 'desc');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.