2

我正在尝试使用以下 url 结构创建 wordpress 自定义帖子类型 (faq) 和分类法 (faq-category):

/faqs/%faq-category%/%postname%

我希望 /faqs 成为所有常见问题解答帖子的存档页面,并且 /faqs/%faq-category% 成为具有给定分类法的所有常见问题解答帖子的存档页面。

但是,常见问题类别是可选的,并非所有常见问题帖子都有一个。在这种情况下,我想要以下 url 结构:

/常见问题解答/%postname%

我能够让 /faqs/%faq-category%/%postname% 返回正确的帖子,并且 /faqs 和 /faqs/%faq-category% 都返回存档页面,但是当我尝试添加没有常见问题的帖子时-选择的类别 (/faqs/%postname%) 我得到 404。

我正在一个 mu 插件中注册我的自定义帖子类型和分类法(我使用的是root.io扩展的 cpt库):

<?php
/**
 * Test Custom CPT
 */
add_action( 'init', function() {

    /**
     * Test custom taxonomy
     */
    register_extended_taxonomy( 'faq-category', 'faq', array(

        # Use radio buttons in the meta box for this taxonomy on the post editing screen:
        'meta_box' => 'radio',

        # Show this taxonomy in the 'At a Glance' dashboard widget:
        'dashboard_glance' => true,

        # Set archive:
        'has_archive' => 'faq-category',

        # Show all posts on the post type archive:
        'archive' => array(
            'nopaging' => true
        ),


    ), array(

        # Override the base names used for labels:
        'singular' => 'FAQ Category',
        'plural'   => 'FAQ Categories',
        'slug'     => 'faq-category'

    ) );

    /**
     * Test custom post type
     */
    register_extended_post_type( 'faq', array(

        # Set icon
        'menu_icon' => 'dashicons-products',

        # Set query var
        'query_var' => true,

        # Make cpt public
        'public' => true,

        # Make cpt queryable
        'publicly_queryable' => true,

        # Non hierarchical post type
        'hierarchical' => false,

        # Add the post type to the site's main RSS feed
        'show_in_feed' => true,

        # Set archive
        'has_archive' => 'faqs',

        # Allow Gutenberg editor
        'show_in_rest' => true,

        # Show all posts on the post type archive:
        'archive' => array(
            'nopaging' => true
        ),

        # Add supports to cpt
        'supports' => array('title', 'taxonomy', 'editor', 'author', 'comments'),

    ), array(

        # Override the base names used for labels:
        'singular' => 'FAQ',
        'plural' => 'FAQs',
        'slug' => 'faq'

    ) );

} );

我还在 mu-plugin 中创建了一个自定义永久链接结构和关联的过滤器:

// Add our custom permastructures for custom taxonomy and post
add_action('init', 'add_faq_permastructure');

function add_faq_permastructure()
{
    global $wp_rewrite;

    add_permastruct('faq-category', 'faqs/%faq-category%', false);
    add_permastruct('faq', 'faqs/%faq-category%/%faq%', false);

}

// Make sure that all links on the site, include the related taxonomy terms
add_filter('post_type_link', 'faq_permalinks', 10, 2);

function faq_permalinks($permalink, $post)
{
    if ($post->post_type !== 'faq')
        return $permalink;

    $terms = get_the_terms($post->ID, 'faq-category');

    if (!$terms)
        return str_replace('%faq-category%/', '', $permalink);

    $post_terms = array();
    foreach ($terms as $term)
        $post_terms[] = $term->slug;

    return str_replace('%faq-category%', implode(',', $post_terms), $permalink);
}

以下是我用来实现这一目标的一些资源: https ://gist.github.com/kasparsd/2924900 https://discourse.roots.io/t/category-listing-page-with-custom-taxonomy /12848/16

4

0 回答 0