wordpress添加页面_如何在WordPress中向页面添加节选

wordpress添加页面

Do you want to add excerpts to your WordPress pages? By default, excerpts in WordPress are only available for posts. In this article, we will show you how to add excerpts to your pages in WordPress.

您想将摘录添加到WordPress页面吗? 默认情况下,WordPress中的摘录仅适用于帖子。 在本文中,我们将向您展示如何在WordPress页面中添加摘录。

How to add excerpts to your pages in WordPress
为什么要在WordPress页面中添加节选? (Why You May Want to Add Excerpts to Pages in WordPress?)

WordPress comes with posts and pages as two default content types. Posts are displayed in reverse chronological order (latest to oldest) on your blog or homepage.

WordPress随附帖子和页面作为两种默认内容类型。 帖子以相反的时间顺序显示(从最新到最旧)在您的博客或主页上。

Pages on the other hand are stand alone content not published in a time specific order. They are typically used for one-off content like your about us or contact page.

另一方面,页面是未按特定时间顺序发布的独立内容。 它们通常用于一次性内容,例如您的关于我们或联系页面

Sometimes you may need to display excerpts for your pages. Specially if you have built a site using only pages.

有时您可能需要显示页面摘录。 特别是如果您仅使用页面构建站点。

Let’s take a look at how to add excerpts to your pages in WordPress as well as how to display those excerpts on your site.

让我们看一下如何在WordPress的页面中添加摘录,以及如何在您的网站上显示这些摘录。

在WordPress页面中添加节选 (Adding Excerpts to Pages in WordPress)

First you need to add the following code to your theme’s functions.php file or a site-specific plugin.

首先,您需要将以下代码添加到主题的functions.php文件或特定站点的插件中

add_post_type_support( 'page', 'excerpt' );

This code modifies the default WordPress content type ‘page’ to add support for excerpts.

此代码修改了默认的WordPress内容类型“页面”,以添加对摘录的支持。

You can head over to create a new page or edit an existing page. Below the post editor, you will be able to see the excerpt meta box.

您可以直接创建新页面或编辑现有页面。 在帖子编辑器下方,您将能够看到摘录的meta框。

Excerpt metabox on the page edit screen in WordPress

Now you can use this excerpt meta box to add custom excerpts for your pages in WordPress.

现在,您可以使用此摘录元框为WordPress中的页面添加自定义摘录。

在WordPress中显示页面摘录 (Displaying Excerpts for Pages in WordPress)

There are many different ways to display excerpts for your pages in WordPress. Depending on what you are trying to do on your website, you can choose the method that best suits your need.

有许多种方法可以显示WordPress中页面的摘录。 根据您要在网站上执行的操作,可以选择最适合您需要的方法。

方法1:使用短码显示带有摘录的最近页面 (Method 1: Display Recent Pages With Excerpts Using Shortcode)

This method allows you to create your own custom queries and display recent pages using a shortcode.

此方法使您可以创建自己的自定义查询,并使用简码显示最近的页面。

First you will need to install and activate the Display Posts Shortcode plugin. For more details, see our step by step guide on how to install a WordPress plugin.

首先,您将需要安装并激活Display Posts Shortcode插件。 有关更多详细信息,请参阅有关如何安装WordPress插件的分步指南。

Upon activation, you need to edit the post, page, or widget where you want to display recent pages and add the following shortcode.

激活后,您需要编辑要显示最近页面的帖子,页面或窗口小部件,并添加以下短代码。


[display-posts post_type="page" include_excerpt="true" excerpt_more="Continue Reading" excerpt_more_link="true"]

This shortcode will display the 10 recent pages with their title, excerpt, and a continue reading link.

该简码将显示最近的10页,以及它们的标题,摘录和继续阅读链接。

If you didn’t enter the custom excerpt for a page, then it will automatically generate the excerpt for the page with the default length of 55 words.

如果您没有输入页面的自定义摘录,则它将自动为页面生成摘录,默认长度为55个字。

Display page excerpts

If you are using the shortcode in a sidebar widget, then you may need to enable shortcode support for text widget. Simply add this code in your theme’s functions.php file.

如果您在侧边栏小部件中使用简码,则可能需要启用对文本小部件的简码支持 。 只需将此代码添加到主题的functions.php文件中。


// Enable shortcodes in text widgets
add_filter('widget_text','do_shortcode');

方法2:在侧边栏中使用插件显示页面摘录 (Method 2: Display Page Excerpts in Sidebar Using Plugin)

This method allows you to easily display recent pages and their excerpts in your theme’s sidebar.

使用此方法,您可以轻松在主题的侧边栏中显示最近的页面及其摘录。

First you need to install and activate the Ultimate Posts Widget. For more details, see our step by step guide on how to install a WordPress plugin.

首先,您需要安装并激活Ultimate Posts Widget 。 有关更多详细信息,请参阅有关如何安装WordPress插件的分步指南。

Upon activation, you need to visit Appearance » Widgets page and add the Ultimate Posts Widget to a sidebar. First you need to provide a title for the widget and then switch to the Display tab.

激活后,您需要访问Appearance»Widgets页面,并将Ultimate Posts Widget添加到侧栏。 首先,您需要为小部件提供标题,然后切换到“显示”选项卡。

Display excerpt for pages in widget

Next, you need to select the ‘Show excerpt’ option and then switch to the filter tab.

接下来,您需要选择“显示摘录”选项,然后切换到过滤器标签。

On the filter tab, select ‘Page’ under the post types section and then click on the save button to store your settings.

在过滤器选项卡上,在帖子类型部分下选择“页面”,然后单击“保存”按钮以存储您的设置。

Select page as post type

You can now visit your website to see the widget in action.

现在,您可以访问您的网站以查看正在运行的小部件。

方法3:手动显示页面节选 (Method 3: Display Page Excerpts Manually)

Another way to display page excerpts is by adding the code directly to your theme files. You can create a custom page template and add the following code as an starting point.

显示页面摘录的另一种方法是将代码直接添加到主题文件中。 您可以创建一个自定义页面模板,并添加以下代码作为起点。


$args = array(
'post_type' => array( 'page' ),
'posts_per_page' => 10,
); 
// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
	while ( $the_query->have_posts() ) {
		$the_query->the_post();
		echo '<h3>'. get_the_title() . '</h3>';
		the_excerpt();
	}
	/* Restore original Post Data */
	wp_reset_postdata();
} else {
	// no posts found
}

You will need to adjust the code to match your theme templates.

您将需要调整代码以匹配您的主题模板。

That’s all, we hope this article helped you learn how to add excerpts to your pages in WordPress. You may also want to see our guide on how to create custom WordPress layouts with Beaver Builder.

仅此而已,我们希望本文能帮助您学习如何在WordPress页面中添加摘录。 您可能还想看看我们的指南,该指南介绍了如何使用Beaver Builder创建自定义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/plugins/add-excerpts-to-your-pages-in-wordpress/

wordpress添加页面

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值