WordPress

przepisywanie adresów URL – Przepisz zagnieżdżone adresy URL dla niestandardowego typu postu

  • 15 lutego, 2019
  • 5 min read
przepisywanie adresów URL – Przepisz zagnieżdżone adresy URL dla niestandardowego typu postu


Mam problem z zagnieżdżonym łączem bezpośrednim. Mam taką strukturę adresów URL:

Catalog -> Category -> Product

Moje adresy URL to:

  • www.domain.com/catalogs (dla katalogu archiwalnego)
  • www.domain.com/catalogs/%catalog% (dla pojedynczej strony katalogu z listą kategorii)
  • www.domain.com/catalogs/%catalog%/%category% (dla pojedynczej strony kategorii z listą produktów)
  • www.domain.com/catalogs/%catalog%/%category%/%product% (dla strony pojedynczego produktu)

W WP Backoffice i Frontoffice wszystkie adresy URL są poprawne, jedyny problem, jaki mam, to odwiedzanie strony produktu, na przykład:

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

Tam otrzymuję błąd 404. Mój kod jest następujący:

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;
}

Tutaj mój .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

AKTUALIZACJA

Warto przeczytać!  Ponad 100 oszałamiających szablonów witryn internetowych umożliwiających automatyzację i rozwój działalności sprzedawcy

Tutaj obraz z wtyczki „Rewrite Analyzer”:

wprowadź tutaj opis obrazu


Źródło