WP DEVELOP

EP031. “Excerpt 摘要支持、View All Events 链接与头部导航高亮”

首页 WordPress 开发课程 CUSTOM POST TYPE 入门 · EP031
约 13 分钟· #EP031#CUSTOM POST TYPE 入门
🔒 登录后可标记已读

这一讲是收尾性质的「杂项」讲座,处理三件事:一是给博客和 event 的摘要文字换成「手写摘要优先,没有手写摘要才 fallback 到自动截断」的写法(用 has_excerpt() 判断);二是让 event 文章类型支持手写摘要(默认自定义文章类型不带这个功能,要自己加);三是把首页「View All Events」按钮和头部导航的 Events 链接接上真实 URL,并让 Events 链接在 event 相关页面正确高亮成黄色。

涉及文件

  • wp-content/mu-plugins/university-post-types.php (修改,supports 数组加 excerpt
  • wp-content/themes/fictional-university-theme/front-page.php (修改,博客循环和 event 循环都改用 has_excerpt() 判断,View All Events 按钮接上真链接)
  • wp-content/themes/fictional-university-theme/header.php (修改,Events 导航链接接上真链接 + 高亮判断)

代码实现


1. mu-plugins/university-post-types.php —— 加 supports(excerpt + editor)

// wp-content/mu-plugins/university-post-types.php

function university_post_types() {
  register_post_type('event', array(
    'show_in_rest' => true,
    'supports' => array('title', 'editor', 'excerpt'), // 新增:让 event 支持摘要功能
    'rewrite' => array('slug' => 'events'),
    'has_archive' => true,
    'public' => true,
    'labels' => array(
      'name' => 'Events',
      'add_new_item' => 'Add New Event',
      'edit_item' => 'Edit Event',
      'all_items' => 'All Events',
      'singular_name' => 'Event'
    ),
    'menu_icon' => 'dashicons-calendar'
  ));
}

add_action('init', 'university_post_types');

要点:自定义文章类型默认只支持 titleeditor(主内容),想要别的功能(摘要、特色图片等)都要显式写进 supports 数组。这里特别要注意:因为要显式列出 supports,就必须把 editor 也一起列进去,否则编辑画面会退回 Classic Editor(EP029 埋的坑,这里正式踩到)。


2. front-page.php —— 摘要 fallback 逻辑 + View All Events 链接

Event 循环和博客循环都做了同样的改动,把原本直接 wp_trim_words(get_the_content(), 18) 换成「有手写摘要就用手写摘要,没有就 fallback 到截断正文」:

// wp-content/themes/fictional-university-theme/front-page.php
// Upcoming Events 循环里,摘要那一行:

<p><?php if (has_excerpt()) {
    echo get_the_excerpt();
  } else {
    echo wp_trim_words(get_the_content(), 18);
    } ?> <a href="<?php the_permalink(); ?>" class="nu gray">Learn more</a></p>

// View All Events 按钮,href 从占位符 # 改成动态归档链接:
<p class="t-center no-margin"><a href="<?php echo get_post_type_archive_link('event') ?>" class="btn btn--blue">View All Events</a></p>

From Our Blogs 循环也是一样的改法(只是链接文字是「Read more」而不是「Learn more」):

// front-page.php,From Our Blogs 循环里,摘要那一行同样改成:
<p><?php if (has_excerpt()) {
    echo get_the_excerpt();
  } else {
    echo wp_trim_words(get_the_content(), 18);
    } ?> <a href="<?php the_permalink(); ?>" class="nu gray">Read more</a></p>

要点:

  • get_the_excerpt() 而不是 the_excerpt(),因为 the_excerpt() 会自带 <p> 包裹并直接输出,跟这里手动排版的 <p> 结构冲突,会多出不需要的垂直间距。get_the_excerpt() 只是返回值,配合 echo 更可控。
  • 没手写摘要时的自动摘要默认是 55 个单词,这个值可以全局调整,但 Brad 更喜欢用 has_excerpt() + wp_trim_words() 这种按模板自定义长度的写法,而不是动全局设置。

3. header.php —— Events 导航链接与高亮

// wp-content/themes/fictional-university-theme/header.php
// Events 这个 <li>,之前是写死的 <a href="#">Events</a>,改成:

<li <?php if (get_post_type() == 'event') echo 'class="current-menu-item"';  ?>><a href="<?php echo get_post_type_archive_link('event'); ?>">Events</a></li>

跟 About Us / Blog 链接的写法一致:单行 if 不需要花括号,直接在同一行判断当前 get_post_type() 是否等于 event,是的话就输出 current-menu-item 类名让链接变黄。这个判断在 event 归档页和 event 单篇页都成立,所以两种页面下 Events 都会正确高亮。

Hook / Function 速查

名称类型用途
has_excerpt()WP 内建 function判断当前文章是否有手写摘要
get_the_excerpt()WP 内建 function(返回值,需 echo)返回当前文章摘要(手写的,或没手写时的自动截断版本),不像 the_excerpt() 那样自带 <p> 输出
get_post_type()WP 内建 function(返回值)返回当前文章的文章类型 key,用来判断导航高亮
supportsregister_post_type() 参数显式声明这个文章类型支持哪些功能(title/editor/excerpt/thumbnail 等)

常见坑

  • 自定义文章类型默认不带 excerpt 输入框,就算点编辑画面右上角「Screen Options」也看不到勾选项,要先在 register_post_type()supports 里显式加 excerpt
  • supports 数组一旦显式声明,就必须把 editor 也写进去,不然编辑画面会退回 Classic Editor(跟 EP029 提到的坑是同一件事)。
  • the_excerpt() 会自动包一层 <p> 并直接输出,跟手写的 HTML 结构混在一起会出现设计上不该有的空隙,这种情况该用 get_the_excerpt() 手动控制输出位置。

延伸 / 后续讲座会用到

EP032 会引入 ACF 插件加一个真正的 event_date 自定义字段,把首页和归档页写死的日期圈换成动态数据。

Sources

Udemy:

  • Become a WordPress Developer: Unlocking Power With Code — Section 7, EP031