timohubois/post-type-and-taxonomy-archive-pages 问题修复 & 功能扩展

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

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

timohubois/post-type-and-taxonomy-archive-pages

最新稳定版本:2.2.3

Composer 安装命令:

composer require timohubois/post-type-and-taxonomy-archive-pages

包简介

Set the archive for your custom post types to associate them with a specific page and control the permalinks for single custom post type pages and custom taxonomies.

README 文档

README

Post Type and Taxonomy Archive Pages Settings enables to select a page that should interact as archive for custom post types. It also enables to change the slug for custom post type single pages or custom taxonomies.

The Plugin extends the native Reading and Permalinks settings pages:

  • Settings > Reading > Adds a section to select a page which should interact as archive for a custom post type.
  • Settings > Permalinks > Adds a section to change the slug for custom post types and custom taxonomies.

How to programmatically get the post used as the archive page for a custom post type?

Example how to retrieve the post object of the page set as the archive for a custom post type:

/**
 * Retrieves the post object for a given post type's archive page.
 *
 * @param string|null $postType The post type to retrieve the archive page for.
 * @return WP_Post|null WP_Post on success, or null on failure.
 */
function getCustomPostTypeArchivePage(?string $postType = null): ?\WP_Post
{
    $postType = $postType ?? getCurrentQueryPostType();

    if ($postType === null) {
        return null;
    }

    $postTypeObject = get_post_type_object($postType);

    if (!$postTypeObject || !$postTypeObject->has_archive) {
        return null;
    }

    $archiveSlug = $postTypeObject->has_archive;

    if (!is_string($archiveSlug)) {
        return null;
    }

    $archivePage = get_page_by_path($archiveSlug);

    return ($archivePage instanceof \WP_Post) ? $archivePage : null;
}

/**
 * Retrieves the current post type from the global $wp_query object.
 *
 * @return string|null The current post type, or null if not found.
 */
function getCurrentQueryPostType(): ?string
{
    global $wp_query;
    return $wp_query->query['post_type'] ?? null;
}

$archivePage = getCustomPostTypeArchivePage('your_custom_post_type');
if ($archivePage) {
    echo "Archive page title: " . $archivePage->post_title;
    echo "Archive page ID: " . $archivePage->ID;
    echo "Archive page URL: " . get_permalink($archivePage->ID);
} else {
    echo "No archive page found for this post type.";
}

Requirements

  • PHP >= 8.0

Installation

  1. Make sure you have the correct requirements.
  2. Clone the repository and place it in wp-content/plugins/ folder.

Development

  1. Make sure you have the correct requirements.
  2. Perform Installation.
  3. Run composer i to install composer dependency.

License

GPLv3

统计信息

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

GitHub 信息

  • Stars: 7
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

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