如何在WordPress中显示最近的帖子

Do you want to show your recent posts in WordPress? Displaying recent posts help users easily discover your new content and spend more time on your site.

您想在WordPress中显示您最近的帖子吗? 显示最新帖子可帮助用户轻松发现您的新内容,并在您的网站上花费更多时间。

You can add a list of your latest posts in your sidebar, after the end of your post content, inside your post content with a shortcode, in your footer widget area, or anywhere else you like.

您可以在侧边栏中,帖子内容结束之后,带有短代码的帖子内容内部,页脚小部件区域或其他任何喜欢的地方添加最新帖子列表。

In this article, we will show you how to display recent posts in WordPress using a variety of different methods

在本文中,我们将向您展示如何使用各种不同的方法显示WordPress中的最新帖子

Displaying recent posts in WordPress
为什么要在WordPress中添加最新帖子? (Why Add Recent Posts in WordPress?)

If you have been running a WordPress blog or website for a long time, then you may have some older articles getting regular traffic from search engines and other sources.

如果您长期运行WordPress博客或网站,则可能会有一些较旧的文章从搜索引擎和其他来源获得常规访问量。

Many of these users will check out the information they were looking for and leave. More than 75% of those visitors will never see your website again.

这些用户中有许多将签出他们正在寻找并离开的信息。 超过75%的访问者将永远不会再看到您的网站。

To keep users coming back to your website, you need to ensure that they spend more time on your website.

为了使用户回到您的网站,您需要确保他们在您的网站上花费更多的时间。

One way to do this is by showing them other recent posts that they should see. This helps users uncover more content on your website which leads to them spending more time on your website.

一种方法是向他们显示他们应该看到的其他最近的帖子。 这有助于用户在您的网站上发现更多内容,从而使他们在您的网站上花费更多的时间。

As users view more pages, this gives you time to convince them into signing up for your email newsletter or making a purchase.

随着用户查看更多页面,这使您有时间说服他们注册电子邮件时事通讯或进行购买。

That being said, let’s take a look at how to easily display recent posts in WordPress using multiple methods. Click on the quick links to jump straight to that method:

话虽如此,让我们看一下如何使用多种方法轻松显示WordPress中的最新帖子。 单击快速链接直接跳至该方法:

影片教学 (Video Tutorial)

演示地址

If you’d prefer written instructions, just keep reading.

如果您喜欢书面说明,请继续阅读。

方法1.使用WordPress的最新文章Gutenberg块 (Method 1. Use WordPress’s Latest Posts Gutenberg Block)

The WordPress block (Gutenberg) editor has a built-in block for displaying your latest posts. You can easily add this to any of the posts or pages on your site.

WordPress块(Gutenberg)编辑器具有一个内置块,用于显示您的最新帖子。 您可以轻松地将此添加到网站上的任何帖子或页面。

To use this block, simply edit the post where you to display recent posts. On the post edit screen, you need to add the ‘Latest Posts’ block to the content area.

要使用此块,只需编辑您要显示最近帖子的帖子。 在帖子编辑屏幕上,您需要将“最新帖子”块添加到内容区域。

Adding the Latest Posts block to a page

There are lots of different ways you can configure this block. By default, it just shows the post titles, but you can easily customize it.

您可以使用许多不同的方法来配置此块。 默认情况下,它仅显示帖子标题,但您可以轻松对其进行自定义。

For example, you can toggle the post content on and off in the right-hand pane. This lets you choose to add post content or excerpt next to the title.

例如,您可以在右侧窗格中打开和关闭帖子内容。 这使您可以选择在标题旁边添加帖子内容或节选。

You can also customize excerpt length which defaults to 55 words, but you can pick any value between 10 and 100 words.

您还可以自定义摘录长度,默认为55个字,但可以选择10到100个字之间的任何值。

Configuring how the post excerpt should display

If you want, you can display the post date. This will appear just below the title.

如果需要,可以显示发布日期。 这将显示在标题下方。

Displaying the dates of your latest posts

You can also choose exactly how your posts will display. You can order them differently from the default ‘Newest to Oldest’, and you can show only the latest posts from a specific category.

您还可以精确选择帖子的显示方式。 您可以按照默认的“最新到最旧”的顺序对它们进行排序,并且只能显示特定类别的最新帖子。

From the settings panel, you can also choose how many posts will display. By default, the block shows 5 posts, but you can display anywhere between 1 and 100 posts.

在设置面板中,您还可以选择显示多少帖子。 默认情况下,该块显示5个帖子,但您可以显示1到100个帖子之间的任意位置。

Sorting and filtering the list of latest posts

You also have the option to display your recent posts in a column grid rather than a list.

您还可以选择在列网格而不是列表中显示最近的帖子。

Setting the latest posts to display in a grid format

You can customize the grid to featured images for your posts, resize the image, and align them accordingly:

您可以自定义网格以显示帖子的特色图片,调整图片大小并相应地对齐它们:

Adding the featured images to your recent posts block

Here’s how the finished block looks live on the website:

以下是完成的代码块在网站上的外观:

Latest posts block displaying live
方法2。使用WordPress最近帖子小部件 (Method 2. Use the WordPress Recent Posts Widget)

What if you want to show a list of your latest posts in your sidebar instead of on a page? WordPress comes with a built-in default widget to display recent posts in your site’s sidebar or any widget ready area.

如果要在侧边栏中而不是页面上显示最新帖子列表怎么办? WordPress带有内置的默认小部件,可在您网站的侧边栏或任何小部件就绪区域中显示最新帖子。

In your WordPress dashboard, go to Appearance » Widgets and add the ‘Recent Posts’ widget to your sidebar.

在WordPress信息中心中,转到外观»小部件,然后将“最新帖子”小部件添加到侧边栏中。

Recent posts widget

The built-in Recent Posts widget doesn’t offer many options. You can give the widget a title, choose whether or not to show the dates of posts, and add the number of posts you want to display.

内置的“最近的帖子”小部件没有很多选择。 您可以给小部件一个标题,选择是否显示帖子的日期,并添加要显示的帖子数。

Once you’re done, click on the ‘Save’ button to store your widget settings.

完成后,单击“保存”按钮以存储小部件设置。

Here’s the widget live on the blog, with the title ‘New Posts’ and dates enabled:

这是博客上的小部件,标题为“新帖子”,并且启用了日期:

The recent posts displaying in the sidebar on the blog
方法3.将“最近发布的窗口小部件”与缩略图插件一起使用 (Method 3. Use the Recent Posts Widget With Thumbnails Plugin)

The built-in widget we mentioned above is quite limited.

我们上面提到的内置小部件非常有限。

What if you want to display thumbnails and excerpts with your recent posts? What if you’d like to only show posts from a specific category in your sidebar?

如果您想在最近的帖子中显示缩略图和摘要,该怎么办? 如果您只想在侧边栏中显示特定类别的帖子怎么办?

Well, that’s when the Recent Posts Widget With Thumbnails plugin comes in handy. It does a lot more than just thumbnails, and it’s free.

好吧,那是“带有缩略图的最近帖子小部件”插件派上用场的时候。 它不仅提供缩略图,而且还免费提供。

First, you’ll need to install and activate the WordPress Recent Posts Widget With Thumbnails plugin.

首先,您需要安装并激活带缩略图的WordPress 最近帖子小部件

Next, go to Appearance » Widgets page where you should see the ‘Recent Posts With Thumbnails’ widget.

接下来,转到外观»小部件页面,您应该在其中看到“带有缩略图的最新帖子”小部件。

The Recent Posts With Thumbnails widget in the list of available widgets

You need to add it to your sidebar or another widget-enabled area.

您需要将其添加到侧边栏或另一个启用小部件的区域。

The Recent Posts With Thumbnails widget in the sidebar

The Recent Posts With Thumbnails widget comes with a lot of options. It gives you full control on how you want to display recent posts on your WordPress site.

“带缩略图的最新帖子”小部件带有很多选项。 它使您可以完全控制要如何在WordPress网站上显示最新帖子。

You can also show excerpts, show posts from specific categories, ignore sticky posts, display random posts, and more.

您还可以显示摘录,显示特定类别的帖子,忽略粘性帖子,显示随机帖子等。

Once you are done, don’t forget to click on the save button to store your settings. You can now visit your website to see your recent posts with thumbnails.

完成后,请不要忘记单击保存按钮来存储您的设置。 现在,您可以访问您的网站,以查看最近的带有缩略图的帖子。

Display Recent Posts in Sidebar
方法4.使用简码显示最近的帖子 (Method 4. Display Recent Posts Using a Shortcode)

Using the ‘Latest Posts’ block is definitely the easiest way to display recent posts in WordPress posts and pages. However, if you chose to disable Gutenberg and keep the Classic editor, you can use shortcodes instead.

使用“最新帖子”块绝对是在WordPress帖子和页面中显示最新帖子的最简单方法。 但是,如果选择禁用Gutenberg并保留Classic编辑器 ,则可以改用简码。

First, install and activate the Display Posts plugin. It works out of the box, and there are no settings for you to configure.

首先,安装并激活Display Posts插件。 它开箱即用,没有可供您配置的设置。

Simply edit a post or page where you want to display your recent posts. Next, use the shortcode [display-posts]. The plugin offers a whole range of parameters that you can use with the shortcode.

只需在想要显示最近帖子的帖子或页面上进行编辑即可。 接下来,使用简码[display-posts] 。 该插件提供了可与简码一起使用的全部参数。

Here are some examples of what you can do using this plugin.

以下是使用此插件可以执行的操作的一些示例。

Display 3 recent posts with thumbnails and excerpt:

显示3条最近的帖子,包括缩略图和节选:

[display-posts posts_per_page="3" image_size="thumbnail" include_excerpt="true"]

Display recent pages instead of posts:

显示最近的页面而不是帖子:

[display-posts posts_per_page="3" post_type="page"]

Change the order to title instead of date:

将顺序更改为标题而不是日期:

[display-posts posts_per_page="3" orderby="title"]

Display recent pages under a specific parent page:

在特定的父页面下显示最近的页面:

[display-posts posts_per_page="3" post_type="page" post_parent="5"]

You can also include additional styling. For instance, you may want to align your featured images to the left. You can do that using the instructions on the plugin’s website here.

您还可以包括其他样式。 例如,您可能想将特色图像向左对齐。 您可以按照此插件网站上的说明进行操作。

Here’s how our list of recent posts looked, using those instructions:

使用这些说明,我们的近期帖子列表如下所示:

Displaying recent posts using the shortcode widget

For a full list of parameters visit the plugin’s documentation.

有关参数的完整列表,请访问插件的文档

You can also use these shortcodes inside a text widget. First, you will need to enable shortcodes in your text widgets by adding this code to your theme’s functions.php file or a site specific plugin.

您也可以在文本窗口小部件内使用这些短代码。 首先,您需要通过将文本代码添加到主题的functions.php文件或特定站点的插件中来在文本小部件中启用短代码。


add_filter('widget_text', 'do_shortcode');

方法5:在WordPress中手动显示最近的帖子 (Method 5. Display Recent Posts Manually in WordPress)

More advanced WordPress users may want to add recent posts directly in their WordPress theme files. Of course, you should use a child theme for this so that when you update your theme, you don’t lose your changes.

更高级的WordPress用户可能希望将最新帖子直接添加到他们的WordPress主题文件中。 当然,您应该为此使用子主题 ,以便在更新主题时不会丢失所做的更改。

It’s always a good idea to create a backup before you edit your theme files. If anything does go wrong, you might want to take a look at our list of the most common WordPress errors and how to fix them.

在编辑主题文件之前创建备份始终是一个好主意。 如果有任何问题,您可能需要查看我们最常见的WordPress错误列表以及如何修复它们

The easiest way to manually display recent posts to use the built-in WP_Query class. Simply add this code where you want to display the recent posts.

手动显示最新帖子以使用内置WP_Query类的最简单方法。 只需将此代码添加到要显示最新帖子的位置。


<ul>

<?php 
// Define our WP Query Parameters
$the_query = new WP_Query( 'posts_per_page=5' ); ?>
 

<?php 
// Start our WP Query
while ($the_query -> have_posts()) : $the_query -> the_post(); 
// Display the Post Title with Hyperlink
?>
 

<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
 

<li><?php 
// Display the Post Excerpt
the_excerpt(__('(more…)')); ?></li>
 

<?php 
// Repeat the process and reset once it hits the limit
endwhile;
wp_reset_postdata();
?>
</ul>

This code displays the five most recent posts with their title and excerpt. The WP_Query class has tons of parameters that allow you to customize it any way that you like. For more information please refer to the WordPress developer documentation.

该代码显示五个最新的帖子及其标题和摘录。 WP_Query类具有大量参数,可让您以自己喜欢的任何方式对其进行自定义。 有关更多信息,请参阅WordPress开发人员文档

We hope that this article helped you learn how to display recent posts in WordPress. You might also be interested in our article on how to create a separate page for blog posts in WordPress, and our comparison of the best drag and drop WordPress page builder plugins.

我们希望本文能帮助您学习如何显示WordPress中的最新帖子。 您可能还对我们有关如何在WordPress中为博客文章创建单独页面的文章以及我们对最佳拖放WordPress页面构建器插件的比较感兴趣。

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在TwitterFacebook上找到我们。

翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-display-recent-posts-in-wordpress/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值