n5s/page-for-custom-post-type 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

n5s/page-for-custom-post-type

Composer 安装命令:

composer require n5s/page-for-custom-post-type

包简介

Page for custom post types, just like page for posts

README 文档

README

QA Coverage PHPStan Packagist Downloads

Assign any WordPress page as the archive page for a custom post type — just like the native "page for posts" setting.

The problem

WordPress custom post type archives are dynamically generated and can't be edited as regular pages. This creates recurring issues:

  • No editable content: clients can't add a title, excerpt, cover image, or custom fields to archive pages.
  • No SEO control: archive pages lack the metadata management that pages offer through SEO plugins.
  • No page builder support: archive templates can't leverage the page editor or custom fields.

Several plugins have attempted to solve this:

While these provided inspiration, none fully replicate WordPress's native behavior.

Approach

This plugin mimics how WordPress handles the posts page (show_on_front=page, page_for_posts={id}). On a posts page request, $wp_query contains both:

  • $wp_query->queried_object — the page itself (WP_Post)
  • $wp_query->posts — the post type's posts (WP_Post[])

This plugin replicates this exact behavior for custom post types, with no extra queries or new functions needed to get your page object.

Setup

Once activated, your public custom post types appear in Settings > Reading.

Settings > Reading

Select any published page to serve as the archive page for each custom post type.

Use page slug as rewrite slug

An optional checkbox lets you use the assigned page's slug as the post type's rewrite slug. This means your archive URL and single post URLs will share the same base path (e.g., /products/ for the archive and /products/my-product/ for a single post).

Warning: Enabling this option changes all single post URLs for the post type. Consider the SEO implications before toggling it on an existing site.

Slug-change protection

Once "Use page slug as rewrite slug" is on, changing the page slug breaks every published URL for that custom post type. The plugin guards both editing surfaces:

  • Block editor: a warning notice and confirmation checkbox appear in the document sidebar; the Update button stays disabled until the checkbox is ticked.
  • Quick Edit on the Pages list: a confirmation dialog blocks the save until acknowledged.

Both only fire when the slug actually differs from the saved value, on pages assigned to a CPT with "use page slug" enabled.

Key differences with native CPT archives

CPT archive Page for CPT
Conditionals is_post_type_archive = true
is_archive = true
is_home = true
is_{posttype}_page = true
is_page_for_custom_post_type = $posttype
Queried object WP_Post_Type WP_Post
Template hierarchy archive-{posttype}.php
archive.php
index.php
home-{posttype}.php
home.php
index.php

API

Functions

// Check if the current page is a "page for custom post type"
is_page_for_custom_post_type(?string $postType = null): bool

// Get the custom post type associated with a page ID
get_custom_post_type_for_page(int $pageId): ?string

// Get the page ID assigned to a custom post type
get_page_id_for_custom_post_type(?string $postType = null): ?int

// Get the URL for a custom post type's archive page
get_page_url_for_custom_post_type(?string $postType = null): ?string

All functions are available both in the n5s\PageForCustomPostType namespace and in the global namespace.

Query properties

// The post type slug, or false if not a PFCPT page
$wp_query->is_page_for_custom_post_type

// Boolean for a specific post type (e.g., is_product_page)
$wp_query->is_{posttype}_page

Hooks

Filters

Filter Description
pfcpt/page_ids Modify the array of page ID / post type mappings
pfcpt/post_type_from_id/page_id Filter page ID resolution for a post type
pfcpt/dropdown_page_args Customize the page dropdown arguments in Settings

Actions

Action Description
pfcpt/template_redirect Fires on template_redirect when on a PFCPT page
pfcpt/flush_rewrite_rules Fires before rewrite rules are flushed

Integrations

Polylang

Full multilingual support:

  • Each language can have its own assigned page
  • Archive URLs are automatically translated
  • Page slugs are translated when using the "use page slug" option
  • Settings dropdown only shows pages in the default language

Requires Polylang 3.4+.

Yoast SEO (WordPress SEO)

  • Full SEO metadata support on archive pages
  • Correct breadcrumb trails (archive page appears in single post and taxonomy breadcrumbs)
  • Proper CollectionPage schema markup
  • Pages are indexed as pages, not archives

Requires Yoast SEO 26+.

WPML

Multilingual support via WPML's String Translation:

  • Each language can have its own assigned page
  • Archive URLs follow WPML's URL structure
  • Page slugs are translatable when using the "use page slug" option

Requires WPML 4.5+.

The SEO Framework

  • SEO metadata support
  • Correct breadcrumb trails
  • Proper query type detection (page, not archive)

Requires The SEO Framework 5.1+.

Advanced Custom Fields

  • Adds a is_page_for_custom_post_type location rule
  • Allows field groups to be conditionally displayed on PFCPT pages

Requires ACF 6+.

Requirements

  • WordPress 6.0+
  • PHP 8.2+

Installation

Composer (recommended)

composer require n5s/page-for-custom-post-type

Manual

Download the latest page-for-custom-post-type-X.Y.Z.zip from the releases page and upload it via Plugins > Add New > Upload Plugin. The zip ships with vendor dependencies bundled, so no build step is required.

Upgrading from 0.x

1.0 makes the "use page slug" behavior opt-in (it was previously always on). On upgrade, the plugin automatically enables it for every CPT that already has a page assigned, so existing URLs stay intact. If you'd rather use the default CPT rewrite slugs going forward, uncheck the option per CPT under Settings > Reading.

The legacy procedural API (is_page_for_custom_post_type(), get_page_id_for_custom_post_type(), etc.) is preserved as deprecated shims that forward to the namespaced equivalents. They'll emit a _doing_it_wrong notice when WP_DEBUG is on.

License

GPL-3.0-or-later

n5s/page-for-custom-post-type 适用场景与选型建议

n5s/page-for-custom-post-type 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 822 次下载、GitHub Stars 达 9, 最近一次更新时间为 2023 年 06 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「wordpress」 「custom post type」 「custom post type archive」 「page for custom post type」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 n5s/page-for-custom-post-type 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 n5s/page-for-custom-post-type 我们能提供哪些服务?
定制开发 / 二次开发

基于 n5s/page-for-custom-post-type 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 822
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 9
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 3
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2023-06-21