achttienvijftien/wp-primary-term
Composer 安装命令:
composer require achttienvijftien/wp-primary-term
包简介
Primary term selector for WP taxonomies
README 文档
README
Primary term selector for WordPress taxonomies.
Installation (Development)
Install PHP dependencies:
composer install
Install JavaScript dependencies:
yarn
Lint
yarn lint:js && yarn lint:php
Format code:
yarn format:js && yarn format:php
Testing
The PHP test suite consists of WordPress integration tests (WP_UnitTestCase)
and runs inside the wp-env test environment.
Start the environment (first run pulls the Docker images):
yarn wp-env start
Run the tests:
yarn test
This runs PHPUnit inside the tests-cli container against the mounted plugin.
The WordPress test library is provided by wp-env (WP_TESTS_DIR); PHPUnit and
the PHPUnit Polyfills are installed as Composer dev dependencies, so make sure
composer install has been run first.
Building
Build for production:
yarn build
Watch mode for development:
yarn start
Usage
Enable Primary Term for Taxonomies
Use the achttienvijftien_primary_term_taxonomies filter to enable primary term functionality:
add_filter( 'achttienvijftien_primary_term_taxonomies', function( $taxonomies ) { $taxonomies[] = 'category'; $taxonomies[] = 'post_tag'; return $taxonomies; } );
Global Functions
All functions take the taxonomy slug first; the post ID is optional and defaults to the current post.
Check whether a post has a primary term (bool):
if ( has_primary_term( 'category', $post_id ) ) { // ... }
Get the primary term ID (int, or WP_Error for an invalid taxonomy):
$term_id = get_primary_term_id( 'category', $post_id );
Get the primary term object (WP_Term, WP_Error, or null when none resolves):
$term = get_primary_term( 'category', $post_id ); if ( $term instanceof \WP_Term ) { echo esc_html( $term->name ); }
When no primary term is stored, these fall back to the first assigned term. Disable
that fallback with the achttienvijftien_primary_term_use_fallback filter:
add_filter( 'achttienvijftien_primary_term_use_fallback', '__return_false' );
统计信息
- 总下载量: 515
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: proprietary
- 更新时间: 2026-06-08