Yoast SEO 开发者文档

title: "Functional Specification" post_status: publish comment_status: open taxonomy: category: - yoast-developer post_tag: - Llms Txt - Features - Repos


本文档解释了 Yoast SEO 如何生成 llms.txt 文件。

Yoast SEO 的 llms.txt 功能有什么作用?

Yoast SEO 如何选择要包含在 llms.txt 文件中的内容?

文章/页面/自定义文章类型 - Yoast SEO 在 llms.txt 文件中包含最近更新的 5 篇文章/页面/自定义文章类型(文章仅在最近 12 个月内发布过才会被包含) - 此逻辑包括优先考虑基石内容 - 对于页面,如果您在 llms.txt 设置中选择 手动选择页面,则可以覆盖此逻辑 - 如果在“搜索外观”设置中为特定自定义文章类型勾选了“在搜索结果中显示标签”框,则会包含自定义文章类型

分类/标签/自定义分类法术语 - Yoast SEO 包含关联内容最多的 5 个分类/标签 - 如果在“搜索外观”设置中为特定分类法勾选了“在搜索结果中显示术语”框,则会包含自定义分类法的术语

如何删除 llms.txt 文件?

如何创建自己的 llms.txt 文件?

已知限制

过滤器

llms.txt 功能提供了几个可用的过滤器。

更改文件路径根目录的获取方式,以放置 llms.txt 文件

add_filter( 'wpseo_llmstxt_filesystem_path', 'custom_llmstxt_file_path' );

/**
 * 使用 WP_CONTENT_DIR 常量来获取服务器的 Web 根目录,而不是默认方式。
 *
 * @return string 修改后的 Web 根目录。
 */
function custom_llmstxt_file_path() {
    return dirname( WP_CONTENT_DIR );
}

编辑 llms.txt 文件中添加的 BOM 前缀

add_filter( 'wpseo_llmstxt_encoding_prefix', 'custom_llmstxt_encoding_prefix' );

/**
 * 从 llms.txt 文件中移除 UTF-8 BOM 前缀并添加 UTF-16 BOM 前缀。
 *
 * @return string 编辑后的前缀。
 */
function custom_llmstxt_encoding_prefix() {
    return "\xFF\xFE";
}
add_filter( 'wpseo_llmstxt_encoding_prefix', 'custom_llmstxt_encoding_prefix' );

/**
 * 从 llms.txt 文件中移除 UTF-8 BOM 前缀。
 *
 * @return string 被移除的前缀。
 */
function custom_llmstxt_encoding_prefix() {
    return "";
}

Edit the description in the posts lists

## Posts
- [Post with excerpt](http://example.com/post-with-excerpt/): This is a custom excerpt.
- [Post without excerpt](http://example.com/post-without-excerpt/)
<?php

/**
 * Changes the link description in the llms.txt file.
 *
 * @param string $link_description The description of the link.
 * @param string $post_id          The ID of the post that is being added as a link.
 * @param string $post_type        The post type of the post that is being added as a link.
 *
 * @return string The changed link description in the llms.txt file.
 */
function set_custom_llmstxt_link_description( $link_description, $post_id, $post_type ) {
    // Remove excerpt for specific post types.
    if ( in_array( $post_type, [ 'page' ], true ) ) {
      return '';
    }

    // Keep excerpt for other post types but make it more descriptive.
    return 'Custom description for post ID ' . $post_id . ', ' . $link_description;
}

add_filter( 'wpseo_llmstxt_link_description', 'set_custom_llmstxt_link_description', 10, 3 );
## Pages
- [Page with excerpt](http://example.com/page-with-excerpt/)

## Posts
- [Post with excerpt](http://example.com/post-1): Custom description for post ID 1, this is the custom excerpt.