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

设置自定义文章类型的存档,以便与特定页面关联,并控制单个自定义文章类型页面和自定义分类的永久链接。

1.8.5 2024-08-30 14:38 UTC

This package is auto-updated.

Last update: 2024-09-30 15:08:01 UTC


README

“文章类型和分类存档页面设置”插件允许您选择一个页面,使其作为自定义文章类型的存档进行交互。它还允许您更改自定义文章类型单页面或自定义分类的别名。

该插件扩展了原生的“阅读”和“永久链接”设置页面

  • 设置 > “阅读” > 添加一个部分,用于选择应作为自定义文章类型存档进行交互的页面。
  • 设置 > “永久链接” > 添加一个部分,用于更改自定义文章类型和自定义分类的别名。

如何以编程方式获取用作自定义文章类型存档页面的文章?

如何检索设置为自定义文章类型存档的页面文章对象的示例

/**
 * 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.";
}

要求

  • PHP >= 8.0

安装

  1. 确保您有正确的要求
  2. 克隆存储库并将其放置在wp-content/plugins/文件夹中。

开发

  1. 确保您有正确的要求
  2. 执行安装
  3. 运行composer i以安装composer依赖项。

许可

GPLv3