Wordpress - 重写网址

问题描述 投票:0回答:1

我有嵌套永久链接的问题。我有这样的网址结构:

Catalog -> Category -> Product

我的网址是:

  • 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
php wordpress url-rewriting
1个回答
0
投票

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

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选项配置。

© www.soinside.com 2019 - 2024. All rights reserved.