wordpress 通过get_the_excerpt和the_excerpt函数获取文章摘要

本文介绍了WordPress中如何使用the_excerpt()和get_the_excerpt()函数来获取文章摘要。the_excerpt()函数会在页面上输出文章摘要并附加[...]符号,若未手动设置摘要,则默认截取文章前55个字符;get_the_excerpt()则用于将文章摘要赋值给变量。这两个函数都只能在主循环中使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

标签: 

the_excerpt

 

获取摘要

 

获取摘录

 

摘要区域

分类: wordpress
## 本文章已经迁移到新的博客:
## 

the_excerpt()  输出当前文章的摘要,并会附上"[...]",这不是"更多"的链接。如果你没提供一篇文章明确的摘要(后台文章编辑器的可选摘要区域),它将自动摘录文章内容中前55个字作为摘要。而HTML标签和图片都会从摘要内容中去除。此标签必须用在主循环里。

用法:

示例:

eg:

使用条件判断语句判断在分类页或者存档页显示摘要,其他页显示内容



get_the_excerpt()   返回文章摘要赋值给变量。此标签必须在主循环里。如果你仅希望输出文章摘要,你可以直接使用the_excerpt().

用法:

参数:$deprecated 没有要求

返回的值:

1、如果文章没有摘要,它将返回一个空字符串,并随后加上"[...]"。

2、对于密码保护的网页,它将返回一个字符串,默认值是"没有摘要,因为这是一个受保护的文章。”,通过函数定义可以改变文字。

3、如果文章有摘要并且没有密码保护的话,就会返回摘要作为一个字符串。

示例:

get_the_excerpt() 用于检索和存储值在一个变量,没有输出到页面。

eg1:

0

0

阅读 (7046)   评论   (1) 收藏 (0)   转载 (2)   喜欢   打印 举报
已投稿到:
if (!function_exists('get_article_for_category')) { function get_article_for_category( $category, $posts_per_page = 5, $tag = '' ,// 新增参数:标签(tag) $include_meta = true, $include_acf = true, $orderby = 'menu_order', $order = 'DESC' ) { // 限制只允许的排序字段 $allowed_orderby = array('date', 'menu_order'); if (!in_array($orderby, $allowed_orderby)) { $orderby = 'date'; // 默认回退到按时间排序 } // 设置基础查询参数 $args = array( 'posts_per_page' => $posts_per_page, 'orderby' => $orderby, 'order' => $order, ); // 添加分类查询条件 if (is_numeric($category)) { $args['tax_query'][] = array( 'taxonomy' => 'category', 'field' => 'term_id', 'terms' => $category, ); } else { $args['tax_query'][] = array( 'taxonomy' => 'category', 'field' => 'slug', 'terms' => $category, ); } if (!empty($tag)) { $args['tax_query']['relation'] = 'AND'; if (is_numeric($tag)) { $args['tax_query'][] = array( 'taxonomy' => 'post_tag', 'field' => 'term_id', 'terms' => $tag, ); } else { $args['tax_query'][] = array( 'taxonomy' => 'post_tag', 'field' => 'slug', 'terms' => $tag, ); } } if ($orderby === 'menu_order') { $args['orderby'] = 'menu_order'; } $query = new WP_Query($args); if ($query->have_posts()) { $posts = array(); while ($query->have_posts()) { $query->the_post(); $post_id = get_the_ID(); $post_data = array( 'id' => $post_id, 'title' => get_the_title(), 'permalink' => get_the_permalink(), 'excerpt' => get_the_excerpt(), 'content' => get_the_content(), 'thumbnail' => has_post_thumbnail() ? get_the_post_thumbnail_url() : '', 'date' => get_the_date(), 'author' => get_the_author(), 'modified_date' => get_the_modified_date(), 'type' => get_post_type(), 'status' => get_post_status(), 'comment_count' => get_comments_number(), ); // 获取所有自定义字段(post meta) if ($include_meta) { $meta = get_post_meta($post_id); $post_data['meta'] = $meta; } // 如果使用了 ACF,获取 ACF 字段 if ($include_acf && function_exists('get_fields')) { $acf_fields = get_fields(); if ($acf_fields) { $post_data['acf'] = $acf_fields; } } $posts[] = $post_data; } wp_reset_postdata(); return $posts; } return false; } }
最新发布
07-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值