跳转到内容

WordPress 开发者如何理解 EmDash

EmDash 将熟悉的 WordPress 概念——文章、页面、分类法、菜单、小工具和媒体库——带入了现代化的 Astro 技术栈。您的内容管理知识可以直接迁移过来。

如果你还在比较方案,建议先阅读 EmDash 与 WordPress 对比 和 如何从 WordPress 迁移到 Astro + EmDash。

您在 WordPress 中熟知的概念,在 EmDash 中同样是一等公民:

  • 集合 类似于自定义文章类型——定义您的内容结构,在模板中查询它
  • 分类法 工作方式相同——层级式(如分类)和扁平式(如标签)
  • 菜单 支持拖拽排序和嵌套项目
  • 小工具区域 用于侧边栏和动态内容区域
  • 媒体库 支持上传、组织和图片管理
  • 管理界面 内容编辑者无需接触代码即可使用

实现方式改变了,但心智模型保持不变:

使用 TypeScript 而非 PHP

模板是 Astro 组件。语法更简洁,但概念相同:都是输出 HTML 的服务端代码。

内容 API 替代 WP_Query

查询函数如 getEmDashCollection() 替代了 WP_Query。无需 SQL,只需函数调用。

基于文件的路由

src/pages/ 目录中的文件成为 URL。无需记忆重写规则或模板层级。

组件替代模板部件

导入并使用组件。与 get_template_part() 理念相同,但组织方式更好。

WordPressEmDash说明
自定义文章类型集合通过管理界面或 API 定义
WP_QuerygetEmDashCollection()过滤器、限制、分类法查询
get_post()getEmDashEntry()返回条目或 null
分类/标签分类法保留层级支持
register_nav_menus()getMenu()一流的菜单支持
register_sidebar()getWidgetArea()一流的小工具区域支持
bloginfo('name')getSiteSetting("title")站点设置 API
the_content()<PortableText />结构化内容渲染
短代码Portable Text 块自定义组件
add_action/filter()插件钩子content:beforeSave 等
wp_optionsctx.kv键值存储
主题目录src/ 目录组件、布局、页面
functions.phpastro.config.mjs + EmDash 配置构建时和运行时配置

WordPress 查询使用 WP_Query 或辅助函数。EmDash 使用类型化的查询函数。

archive.php
<?php
$posts = new WP_Query([
'post_type' => 'post',
'posts_per_page' => 10,
'post_status' => 'publish',
'category_name' => 'news',
]);
while ($posts->have_posts()) :
$posts->the_post();
?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php endwhile; ?>
single.php
<?php
$post = get_post($id);
?>
<article>
<h1><?php echo $post->post_title; ?></h1>
<?php echo apply_filters('the_content', $post->post_content); ?>
</article>

WordPress 使用模板层级来选择渲染页面的文件。Astro 使用显式的基于文件的路由。

WordPress 模板EmDash 等效路径
index.phpsrc/pages/index.astro
single.phpsrc/pages/posts/[slug].astro
single-{type}.phpsrc/pages/{type}/[slug].astro
page.phpsrc/pages/pages/[slug].astro
archive.phpsrc/pages/posts/index.astro
archive-{type}.phpsrc/pages/{type}/index.astro
category.phpsrc/pages/categories/[slug].astro
tag.phpsrc/pages/tags/[slug].astro
search.phpsrc/pages/search.astro
404.phpsrc/pages/404.astro
header.php / footer.phpsrc/layouts/Base.astro
sidebar.phpsrc/components/Sidebar.astro

WordPress 的模板部件变成了 Astro 组件:

functions.php / template
// In template:
get_template_part('template-parts/content', 'post');
// template-parts/content-post.php:
<article class="post">
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</article>

EmDash 提供一流的菜单支持,并自动解析 URL:

header.php
<?php
wp_nav_menu([
'theme_location' => 'primary',
'container' => 'nav',
]);
?>

菜单可以通过管理界面、种子文件或 WordPress 导入来创建。

小工具区域的工作方式类似于 WordPress 中的侧边栏:

sidebar.php
<?php if (is_active_sidebar('sidebar-1')) : ?>
<aside>
<?php dynamic_sidebar('sidebar-1'); ?>
</aside>
<?php endif; ?>

站点选项和自定义器设置映射到 getSiteSetting():

WordPressEmDash
bloginfo('name')getSiteSetting("title")
bloginfo('description')getSiteSetting("tagline")
get_custom_logo()getSiteSetting("logo")
get_option('date_format')getSiteSetting("dateFormat")
home_url()Astro.site
import { getSiteSetting } from "emdash";
const title = await getSiteSetting("title");
const logo = await getSiteSetting("logo"); // Returns { mediaId, alt, url }

分类法在概念上工作方式相同——层级式(如分类)或扁平式(如标签):

import { getTaxonomyTerms, getEntryTerms, getTerm } from "emdash";
// Get all categories
const categories = await getTaxonomyTerms("categories");
// Get a specific term
const news = await getTerm("categories", "news");
// Get terms for a post
const postCategories = await getEntryTerms("posts", postId, "categories");

WordPress 钩子 (add_action, add_filter) 变为 EmDash 插件钩子:

WordPress 钩子EmDash 钩子用途
save_postcontent:beforeSave保存前修改内容
the_contentPortableText 组件转换渲染后的内容
pre_get_posts查询选项过滤查询
wp_head布局的 <head>添加头部内容
wp_footer布局的 </body> 前添加页脚内容

类型安全

全面使用 TypeScript。集合、查询和组件都是完全类型化的。无需再猜测字段名或返回类型。

性能

没有 PHP 开销。默认静态生成。需要时进行服务端渲染。支持边缘部署。

现代化的开发体验

热模块替换。基于组件的架构。现代化工具链(Vite、TypeScript、ESLint)。

基于 Git 的部署

代码和模板在 git 中。内容在数据库中。无需 FTP,无需文件权限,没有被黑的网站。

EmDash 生成带有 HMAC 签名令牌的安全预览 URL。内容编辑者无需登录生产环境即可预览草稿——分享链接,而非凭据。

WordPress 插件冲突不复存在。EmDash 插件在具有明确 API 的隔离上下文中运行。没有全局状态污染。

内容编辑者使用 EmDash 管理面板,类似于 wp-admin:

  • 仪表盘 显示最近活动
  • 集合列表 支持搜索、过滤和批量操作
  • 富文本编辑器 用于内容编辑(Portable Text,而非 Gutenberg)
  • 媒体库 支持拖拽上传
  • 菜单构建器 支持拖拽排序
  • 小工具区域编辑器 用于侧边栏内容

编辑体验是熟悉的。底层技术是现代化的。

EmDash 可以直接导入 WordPress 内容:

  1. 从 WordPress 导出(工具 → 导出)
  2. 在 EmDash 管理界面中上传 .xml 文件
  3. 将文章类型映射到集合
  4. 导入内容和媒体

文章、页面、分类法、菜单和媒体都会迁移。Gutenberg 块会转换为 Portable Text。自定义字段会被分析并映射。

完整说明请参阅 WordPress 迁移指南。