內容簡介
此外掛會創建一個自定義文章類型「Taoxnomy Page」,該類型與一個術語關聯。
如何操作
「分類頁面」將覆蓋術語存檔的第一頁。您可以選擇哪些分類擁有「分類頁面」。
例如:
您已決定「類別」擁有「分類頁面」。
為「Book」類別創建一個「分類頁面」。
以塊狀編輯器編輯「分類頁面」並發布它。
現在「書籍」類別的第 1 頁 /category/book 會顯示「分類頁面」«书页» 的內容。
模板結構
您可以在編輯器中為分類頁面選擇模板,
但是您可以在您的佈景主題中放置 singular-taxonomy-page.php 模板,因此無需進行選擇。
以下是默認模板優先級:
singular-taxonomy-page.php
page.php
singular.php
single.php
index.php
過濾器 hook rich_taxonomy_include_template 也可用。
自定義
存檔區塊
存檔區塊具有以下模板結構。
template-parts
- rich-taxonomy
- archive-block-loop.php // 文章列表循環。
- archive-block-more.php // 鏈接按鈕。
- archive-block-toggle.php // 切換按鈕。
- archive-block-wrapper.php // 存檔的包裝。
如果主題具有相同路徑中的文件,則具有優先權。
複製文件並自定義。
樣式
要覆蓋樣式,有 4 個 hook 可用。
rich_taxonomy_block_asset_style
rich_taxonomy_block_asset_editor_style
rich_taxonomy_block_asset_script
rich_taxonomy_block_asset_editor_script
要更改外觀和感覺,可以從 rich_taxonomy_block_asset_style 開始。
// 註冊樣式。
add_action( 'init', function() {
wp_registeR_style( 'my-archive-block', $url, $deps, $version );
} );
// 覆蓋處理。
add_filter( 'rich_taxonomy_block_asset_style', function( $handle, $block_name ) {
if ( 'rich-taxonomy/arcvhie-block' === $block_name ) {
$handle = 'my-archive-block';
}
return $handle;
}, 10, 2 );
此樣式在公共和編輯器中都會加載。
默認內容
要定義分類頁面的默認內容,請使用 rich_taxonomy_default_post_object 篩選器 hook。
/**
* 篩選默認帖子對象。
*
* @param array $args 通過 wp_insert_post()傳遞的文章對象。
* @param WP_Term $term 指定給此帖子的術語對象。
* @param string $context 目前僅支持 'api'。
*/
add_filter( 'rich_taxonomy_default_post_object', function( $args, $term, $contest ) {
// 如果特定分類,請輸入默認內容。
if ( 'category' === $term->taxonomy ) {
// 帖子內容。
$args['post_content'] = '這裡是默認內容。';
// 立即發佈。
$args['post_status'] = 'publish';
}
return $args;
}, 10, 3 );
外掛標籤
開發者團隊
原文外掛簡介
Replace term archive pages with custom Taxonomy Pages (CPT).
Use the block editor to design archive landing pages.
Includes a Taxonomy Archive Block to display posts.
Fully customizable via templates and filter hooks.
How It Works
The Taxonomy Page will override the first page of a term archive. In Settings you can choose which taxonomies should have the option to create a Taxonomy Page.
For example, to create a Taxonomy Page for the News category:
In Settings → Reading select Category.
Go to Posts → Categories, hover over “News” and click Taxonomy Page.
Edit the Taxonomy Page in the block editor and publish it.
View the page at /category/news (assuming your permalink structure is set to “Post name”).
Taxonomy Archive Block
When editing a Taxonomy Page in the block editor, you also have access to the Taxonomy Archive Block. This block displays an overview of every post in the term archive. A number of options allow you to alter its behavior:
Number of Posts
Sets the maximum number of posts displayed in the overview.
Toggle Button Text
Sets the text for the toggle button. This button appears when the total number of posts exceeds the number set in “Number of Posts”.
Archive Button Text
Sets the text for the archive button. This button links to the second page of the term archive. It will be displayed when the amount of posts exceeds Blog pages show at most in Settings → Reading.
Template Structure
You can choose a template for the Taxonomy Page in the block editor. Alternatively, you can create your own template, by adding singular-taxonomy-page.php to your theme’s templates, or using the filter hook rich_taxonomy_include_template.
The default template hierarchy, from highest to lowest priority, is as follows:
singular-taxonomy-page.php
page.php
single.php
singular.php
index.php
Customization
Template Override: Taxonomy Archive Block
To override the layout of the Taxonomy Archive Block, copy these files into your theme under:
template-parts/rich-taxonomy/
Files:
archive-block-loop.php – Loop of post list
archive-block-more.php – Archive button
archive-block-toggle.php – Toggle button
archive-block-wrapper.php – Wrapper of archive
Styles and Scripts
You can override the plugin’s styles and scripts using these hooks:
rich_taxonomy_block_asset_style
rich_taxonomy_block_asset_editor_style
rich_taxonomy_block_asset_script
rich_taxonomy_block_asset_editor_script
To change the look & feel, rich_taxonomy_block_asset_style is the best starting point.
Example: Override Style
// Register style.
add_action( 'init', function() {
wp_registeR_style( 'my-archive-block', $url, $deps, $version );
} );
// Override handle.
add_filter( 'rich_taxonomy_block_asset_style', function( $handle, $block_name ) {
if ( 'rich-taxonomy/archive-block' === $block_name ) {
$handle = 'my-archive-block';
}
return $handle;
}, 10, 2 );
This style will load on both the front-end and block editor.
Default Contents
To define the default content of the Taxonomy Page, use the rich_taxonomy_default_post_object filter hook.
Example: Define Default Content
/**
* Filter default post object.
*
* @param array $args Post object passed to wp_insert_post().
* @param WP_Term $term Term object assigned to this post.
* @param string $context Currently only 'api' is supported.
*/
add_filter( 'rich_taxonomy_default_post_object', function( $args, $term, $context ) {
// If specific taxonomy, enter default content.
if ( 'category' === $term->taxonomy ) {
// Post body.
$args['post_content'] = 'Here comes default content.';
// Publish immediately.
$args['post_status'] = 'publish';
}
return $args;
}, 10, 3 );
