简体   繁体   English

显示按日期排序的所有帖子

[英]Display all posts ordered by date

By default WordPress homepage shows latest posts. 默认情况下,WordPress主页显示最新帖子。 How can I display the latest posts on a page that is not the homepage ? 如何在不是主页的页面上显示最新帖子?

My first goal "GOAL A" is for the homepage to display a specific category called "popular posts" (instead of latest posts). 我的第一个目标“目标A”是让首页显示一个称为“热门帖子”的类别(而不是最新帖子)。 "GOAL B" is to have a link on the menu to ALL posts ordered by date, aka "the latest posts". “目标B”将在菜单上链接到按日期排序的所有帖子,也就是“最新帖子”。

I have accomplished GOAL A with code below. 我已经用下面的代码完成了目标A。 How can I accomplish "GOAL B" ? 如何完成“目标B”? I can make a category called "New" and make that a link on the menu, but how can I make it display all posts ordered by date ? 我可以创建一个名为“ New”的类别,并在菜单上创建一个链接,但是如何使它显示按日期排序的所有帖子? Or is there a better method ? 还是有更好的方法?

.
"GOAL A" CODE: display specific category on homepage “目标A”代码:在首页上显示特定类别

function popular_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
    $query->set( 'category_name', 'popular' );
    }
}
add_action( 'pre_get_posts', 'popular_category' );

I think the best here will be is to create a static front page with a blog page. 我认为最好的办法是使用博客页面创建静态首页。 This seems to be fit for what you are trying to do 这似乎适合您想要做的事情

Here is how: 方法如下:

STEP 1 第1步

You should delete the code in your question. 您应该删除问题中的代码。 This will not be necessary here 在这里没有必要

STEP 2 第2步

Make a copy of your page.php (or index.php ) and rename it front-page.php . 复制page.php (或index.php )并将其重命名为front-page.php Open it up, and replace the loop with a custom query which will only display posts from the desired category. 打开它,然后用自定义查询替换循环,该查询将仅显示所需类别的帖子。 Unfortunately, pre_get_posts does not work on a static front page, so here you will have to make use of a custom query. 不幸的是, pre_get_posts在静态首页上不起作用,因此在这里您将不得不使用自定义查询。

<?php
// set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;

// the query
$the_query = new WP_Query( 'category_name=popular&posts_per_page=10&paged=' . $paged ); 
?>

<?php if ( $the_query->have_posts() ) : ?>

<?php
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post(); 
?>
<?php the_title(); ?>
<?php endwhile; ?>

<?php

// next_posts_link() usage with max_num_pages
next_posts_link( 'Older Entries', $the_query->max_num_pages );
previous_posts_link( 'Newer Entries' );
?>

<?php 
// clean up after the query and pagination
wp_reset_postdata(); 
?>

<?php else:  ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

Just remember, for paging on a static front page, you have to use page , not paged as you would for all other custom queries. 请记住,要在静态首页上进行分页,您必须使用page ,而不是像其他所有自定义查询一样使用paged

STEP 3 步骤3

Make a copy of your index.php and rename it home.php . 复制index.php并将其重命名为home.php This will be your blog page template 这将是您的博客页面模板

STEP 4 第四步

You can now set your static front page and blog page in the back. 现在,您可以在背面设置静态首页和博客页面。 You should have a read here about setting up a static front page and a blog page 您应该在这里阅读有关设置静态首页和博客页面的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM