重写自定义帖子类型的嵌套URL

时间:2019-02-15 作者:Mintendo

我对嵌套permalink有问题。我的URL结构如下:

Catalog -> Category -> Product

我的URL是:

  • www.domain.com/catalogs (用于存档目录)www.domain.com/catalogs/%catalog% (对于包含类别列表的单个目录页)www.domain.com/catalogs/%catalog%/%category% (对于包含产品列表的单一类别页面)www.domain.com/catalogs/%catalog%/%category%/%product% (对于单个产品页)
在WP Backoffice和Frontoffice中,所有url都是正确的,我唯一的问题是在访问产品页时,例如:

www.domain.com/catalogs/catalog-001/category-001/product-001

在那里,我收到了404错误。我的代码如下:

add_action( \'init\', \'ab_create_catalog_taxonomy\' );

function ab_create_catalog_taxonomy() {
    $labels = array(
        \'name\'              => _x( \'Catalogs\', \'taxonomy general name\', \'autoblok\' ),
        \'singular_name\'     => _x( \'Catalog\', \'taxonomy singular name\', \'autoblok\' ),
        \'search_items\'      => __( \'Search Catalogs\', \'autoblok\' ),
        \'all_items\'         => __( \'All Catalogs\', \'autoblok\' ),
        \'parent_item\'       => __( \'Parent Catalog\', \'autoblok\' ),
        \'parent_item_colon\' => __( \'Parent Catalog:\', \'autoblok\' ),
        \'edit_item\'         => __( \'Edit Catalog\', \'autoblok\' ),
        \'update_item\'       => __( \'Update Catalog\', \'autoblok\' ),
        \'add_new_item\'      => __( \'Add New Catalog\', \'autoblok\' ),
        \'new_item_name\'     => __( \'New Catalog Name\', \'autoblok\' ),
        \'menu_name\'         => __( \'Catalog\', \'autoblok\' ),
    );

    $args = array(
        \'labels\'            => $labels,
        \'hierarchical\'      => false,
        \'public\'            => true,
        \'show_ui\'           => true,
        \'show_admin_column\' => true,
        \'show_in_nav_menus\' => true,
        \'show_tagcloud\'     => false,
        \'query_var\'         => true,
        \'rewrite\'           => array(
            \'slug\' => \'catalogs\',
        ),
    );

    register_taxonomy( \'catalog\', array( \'category\', \'product\' ), $args );
}

add_action( \'init\', \'ab_create_category_post_type\' );

function ab_create_category_post_type() {
    $labels = array(
        \'name\'          => _x( \'Categories\', \'Post Type General Name\', \'autoblok\' ),
        \'singular_name\' => _x( \'Category\', \'Post Type Singular Name\', \'autoblok\' ),
        \'add_new\'       => __( \'Add new\', \'autoblok\' ),
        \'add_new_item\'  => __( \'Add new category\', \'autoblok\' ),
        \'edit_item\'     => __( \'Edit category\', \'autoblok\' ),
        \'new_item\'      => __( \'New category\', \'autoblok\' ),
        \'view_item\'     => __( \'View category\', \'autoblok\' ),
        \'view_items\'    => __( \'View categories\', \'autoblok\' ),
        \'search_items\'  => __( \'Search category\', \'autoblok\' ),
        \'all_items\'     => __( \'All categories\', \'autoblok\' )
    );

    $args = array(
        \'label\'               => __( \'categories\', \'autoblok\' ),
        \'description\'         => __( \'Category List\', \'autoblok\' ),
        \'labels\'              => $labels,
        \'supports\'            => array(
            \'title\',
            \'custom-fields\'
        ),
        \'hierarchical\'        => true,
        \'public\'              => true,
        \'can_export\'          => true,
        \'has_archive\'         => true,
        \'menu_position\'       => 20,
        \'exclude_from_search\' => false,
        \'capability_type\'     => \'page\',
        \'rewrite\'             => array(
            \'slug\' => __( \'catalogs/%catalog%\', \'autoblok\' ),
        ),
    );

    register_post_type( \'category\', $args );
}

add_action( \'init\', \'ab_create_product_post_type\' );

function ab_create_product_post_type() {
    $labels = array(
        \'name\'          => _x( \'Products\', \'Post Type General Name\', \'autoblok\' ),
        \'singular_name\' => _x( \'Product\', \'Post Type Singular Name\', \'autoblok\' ),
        \'add_new\'       => __( \'Add new\', \'autoblok\' ),
        \'add_new_item\'  => __( \'Add new product\', \'autoblok\' ),
        \'edit_item\'     => __( \'Edit product\', \'autoblok\' ),
        \'new_item\'      => __( \'New product\', \'autoblok\' ),
        \'view_item\'     => __( \'View product\', \'autoblok\' ),
        \'view_items\'    => __( \'View products\', \'autoblok\' ),
        \'search_items\'  => __( \'Search product\', \'autoblok\' ),
        \'all_items\'     => __( \'All products\', \'autoblok\' )
    );

    $args = array(
        \'label\'               => __( \'products\', \'autoblok\' ),
        \'description\'         => __( \'Product List\', \'autoblok\' ),
        \'labels\'              => $labels,
        \'supports\'            => array(
            \'title\',
            \'custom-fields\'
        ),
        \'hierarchical\'        => false,
        \'public\'              => true,
        \'can_export\'          => true,
        \'has_archive\'         => true,
        \'menu_position\'       => 20,
        \'exclude_from_search\' => false,
        \'capability_type\'     => \'page\',
        \'rewrite\'             => array(
            \'slug\' => __( \'catalogs/%catalog%/%category%\', \'autoblok\' ),
        ),
    );

    register_post_type( \'product\', $args );
}

add_filter( \'post_type_link\', \'ab_category_post_link\', 1, 3 );

// Rewrite Category Url
function ab_category_post_link( $post_link, $post ) {
    if ( is_object( $post ) && $post->post_type === \'category\' ) {
        $terms = wp_get_object_terms( $post->ID, \'catalog\' );

        if ( $terms ) {
            return str_replace( \'%catalog%\', $terms[0]->slug, $post_link );
        }
    }

    return $post_link;
}

add_action( \'add_meta_boxes\', \'ab_product_meta_boxes\' );

function ab_product_meta_boxes() {
    add_meta_box( \'product-parent\', \'Category\', \'ab_product_attributes_meta_box\', \'product\', \'side\', \'high\' );
}

function ab_product_attributes_meta_box( $post ) {
    $pages = wp_dropdown_pages( array(
        \'post_type\'        => \'category\',
        \'selected\'         => $post->post_parent,
        \'name\'             => \'parent_id\',
        \'show_option_none\' => __( \'(no parent)\' ),
        \'sort_column\'      => \'menu_order, post_title\',
        \'echo\'             => 0
    ) );

    if ( ! empty( $pages ) ) {
        echo $pages;
    }
}

add_filter( \'post_type_link\', \'ab_product_post_link\', 10, 3 );

function ab_product_post_link( $post_link, $post ) {
    if ( is_object( $post ) && $post->post_type === \'product\' ) {
        $parent      = $post->post_parent;
        $parent_post = get_post( $parent );
        $terms       = wp_get_post_terms( $post->ID, \'catalog\' );

        if ( $terms ) {
            $post_link = str_replace( \'%catalog%\', $terms[0]->slug, $post_link );
        }

        $post_link = str_replace( \'%category%\', $parent_post->post_name, $post_link );
    }

    return $post_link;
}
这是我的。htaccess:

# BEGIN WordPress
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\\.php)$ $2 [L]
RewriteRule . index.php [L]
# END WordPress

UPDATE

这里是“重写分析器”插件中的图像:

enter image description here

1 个回复
SO网友:Mintendo

我找到了一个解决方案,感谢您的插件:

Monkeyman重写规则

使用此插件,我可以在规则失败时找到它,然后使用以下代码行更正它:

add_action( \'init\', \'ab_category_rewrite_rules\' );

function ab_category_rewrite_rules() {
    add_rewrite_tag( \'%category%\', \'([^/]+)\', \'category=\' );
    add_permastruct( \'category\', \'/catalogs/%catalog%/%category%\', false );
    add_rewrite_rule( \'^category/([^/]+)/([^/]+)/?$\', \'index.php?category=$matches[2]\', \'top\' );
}

add_action( \'init\', \'ab_product_rewrite_rules\' );

function ab_product_rewrite_rules() {
    add_rewrite_tag( \'%product%\', \'([^/]+)\', \'product=\' );
    add_permastruct( \'product\', \'/catalogs/%catalog%/%category%/%product%\', false );
    add_rewrite_rule( \'^product/([^/]+)/([^/]+)/?$\', \'index.php?product=$matches[2]\', \'top\' );
}
我还删除了rewrite 我的自定义帖子类型注册功能上的选项配置(针对类别和产品类型)。

相关推荐