Wsparcie » Wtyczki » Problem z linkowaniem w archiwum custom post type

  • Witajcie,

    stworzyłem katalog produktów z użyciem custom post type. Praktycznie wszystko działa, ale jak to bywa jest zawsze jakieś ale. Strona katalogu jest zawarta w pliku archive-products i wyświetla kategorie i produkty. Problemem jest linkowanie produktów – na stronie archiwum są widoczne, ale po kliknięciu wyświetla 404, to samo dzieje się po dodaniu nowego przedmiotu i przejściu w podgląd. Ścieżka jaką generuje wp to domena.pl/products/nazwa-produktu, ale po ręcznym dodaniu do ścieżki nazwy kategorii /products/nazwa-kategorii/nazwa-produktu już się wyświetla. Poniżej przesyłam kod z pliku functions

    add_filter('post_type_link', 'products_type_link', 1, 3);
    
    function products_type_link($url, $post = null, $leavename = false){
    
        // products only
        if ($post->post_type != 'products') {
            return $url;
        }
    
        $post_id = $post->ID;
        $taxonomy = 'product-categories';
        $taxonomy_tag = '%' . $taxonomy . '%';
    
        // Check if exists the tag
        if (strpos($taxonomy_tag, $url) < 0) {
            return $url;
        }
    
        // Get the terms
        $terms = wp_get_post_terms($post_id, $taxonomy);
    
        if (is_array($terms) && sizeof($terms) > 0) {
            $category = $terms[0];
        }
    
        // replace taxonomy tag with the term slug: /products/dvd/productname
        return str_replace($taxonomy_tag, $category->slug, $url);
    }
    
    function products_add_rewrite_rules() {
        global $wp_rewrite;
    	global $wp_query;
    
    	register_post_type('products', array(
            'label' => 'Products',
            'description' => 'GVS products and services.',
            'public' => true,
            'show_ui' => true,
            'show_in_menu' => true,
            'capability_type' => 'post',
            'hierarchical' => true,
            'rewrite' => array('slug' => 'products'),
            'query_var' => true,
            'has_archive' => true,
            'menu_position' => 6,
            'supports' => array(
                'title',
                'editor',
                'excerpt',
                'trackbacks',
                'revisions',
                'thumbnail',
                'author'),
            'labels' => array (
                'name' => 'Products',
                'singular_name' => 'product',
                'menu_name' => 'Products',
                'add_new' => 'Add product',
                'add_new_item' => 'Add New product',
                'edit' => 'Edit',
                'edit_item' => 'Edit product',
                'new_item' => 'New product',
                'view' => 'View product',
                'view_item' => 'View product',
                'search_items' => 'Search Products',
                'not_found' => 'No Products Found',
                'not_found_in_trash' => 'No Products Found in Trash',
                'parent' => 'Parent product'),
            )
        );
    
        register_taxonomy('product-categories', 'products', array(
            'hierarchical' => true,
            'label' => 'Product Categories',
            'show_ui' => true,
            'query_var' => true,
            'rewrite' => array('slug' => 'products'),
            'singular_label' => 'Product Category')
        );
    
    add_rewrite_rule("products/?$", 'index.php?post_type=products&product-categories=', 'top');
    
        add_rewrite_rule("products/([^/]+)/?$", 'index.php?post_type=products&product-categories=$matches[1]', 'top');
    
        add_rewrite_rule("products/([^/]+)/([^/]+)/?$", 'index.php?post_type=products&product-categories=$matches[1]&products=$matches[2]', 'top');
    
        add_rewrite_rule("products/([^/]+)/page/([0-9]{1,})/?$", 'index.php?post_type=products&paged=$matches[1]', 'bottom');
    
    	flush_rewrite_rules();
    }
    add_action('init', 'products_add_rewrite_rules');

    Walczę z tym od kilku dni i albo działa linkowanie produktów (po usunięciu add_rewrite_rule), albo kategorii w archiwum produktów.

  • Temat ‘Problem z linkowaniem w archiwum custom post type’ jest zamknięty na nowe odpowiedzi.