• Hello, i am a french beginner developer in WordPress and i am doing a site for an artist. So i created a custom post type “portfolio” like this :

    register_post_type('portfolio', [
    'labels' => [
    'name' => 'Portfolio',
    'show_ui' => true,
    'singular_name' => 'Portfolio Item',
    ],
    'public' => true,
    'has_archive' => true,
    'supports' => ['title', 'thumbnail', 'editor'],
    'rewrite' => ['slug' => 'portfolio'],
    'menu_icon' => 'dashicons-format-gallery'
    ]);
    }
    add_action('init', 'gbl_post_types');

    Then a taxonomy which allows to create categories and sub-categories to my portfolio, in fact 2 parent category and also sub-category, for example (photographie/portrait), so i created this :function create_portfolio_taxonomy() { $labels = array( 'name' => _x( 'Catégories Portfolio', 'taxonomy general name' ), 'singular_name' => _x( 'Catégorie Portfolio', 'taxonomy singular name' ), 'all_items' => ( 'Toutes les Catégories' ), 'parent_item' => ( 'Parent Catégorie Portfolio' ), 'parent_item_colon' => ( 'Parent Catégorie Portfolio:' ), 'edit_item' => ( 'Editer une Catégorie Portfolio' ), 'update_item' => ( 'Mettre à jour une Catégorie Portfolio' ), 'add_new_item' => ( 'Ajouter une Catégorie du Portfolio' ), 'new_item_name' => ( 'Nouvelle Catégorie Portfolio' ), 'menu_name' => ( 'Catégories du Portfolio' ), ); $args = array( 'hierarchical' => true, 'labels' => $labels, 'public' => true, 'show_ui' => true, 'has_archive' => true, 'show_in_rest' => true, 'show_admin_column' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'portfolio-category' ), ); register_taxonomy( 'portfolio_category', array( 'portfolio' ), $args ); } add_action( 'init', 'create_portfolio_taxonomy' );

    Expand

    And i display first my 2 parent categories with the “archive-portfolio.php” template then the sub-categories of a parent category with the “taxonomy-portfolio_category.php” template and the template for display one item of my portfolio is “single-portfolio.php” I would like the permalinks to be good by displaying like that : “my-site/portfolio/category/sub-category/post-title, for example : my-site/portfolio/photographie/portrait-of-woman-in-the-street.

    So the problem comes from my taxonomy-portfolio_category.php code:

    <div class="works-col col-xs-12 col-sm-6 col-md-6 col-lg-6 <?php foreach ($categories as $category) { echo get_sorting_class($category->name) . ' '; } ?>">
        <div class="works-item scrolla-element-anim-1 scroll-animate" data-animate="active">
            <?php
            $parent_category = null;
            $sub_category = null;
    
            // Find the parent and sub-category terms
            foreach ($categories as $category) {
                if ($category->parent == 0) {
                    $parent_category = $category;
                } else {
                    $sub_category = $category;
                }
            }
    
            if ($parent_category) {
                $parent_category_slug = sanitize_title($parent_category->slug);
            } else {
                $parent_category_slug = '';
            }
    
            if ($sub_category) {
                $sub_category_slug = sanitize_title($sub_category->slug);
            } else {
                $sub_category_slug = '';
            }
    
            $portfolio_item_slug = sanitize_title(get_the_title());
    
            // Construct the correct link based on hierarchy
            $link = home_url("/portfolio/$parent_category_slug");
            if (!empty($sub_category_slug)) {
                $link .= "/$sub_category_slug";
            }
            $link .= "/$portfolio_item_slug/";
            ?>
            <a href="<?php echo esc_url($link); ?>">
    <span class="image">
                                    <span class="img">
                                        <img src="<?php echo esc_url($thumbnail_url); ?>" alt="<?php the_title_attribute(); ?>" />
                                    </span>
                                </span>
                                <span class="desc">
                                    <span class="category splitting-text-anim-4 scroll-animate" data-splitting="chars" data-animate="active">
                                    <?php
                                        $category_names = array();
                                        // Sort categories by term ID to ensure consistent order
                                        usort($categories, function ($a, $b) {
                                            return $a->term_id - $b->term_id;
                                        });
                                        foreach ($categories as $category) {
                                            $category_names[] = esc_html($category->name);
                                        }
                                        echo implode(' - ', $category_names);
                                        ?>
                                    </span>
                                </span>
                            </a>
                        </div>
                    </div>
    
    

    Expand

    This code should construct the permalink with the parent category, sub-category, and post title but still not good, i have my-site/portfolio/photographie/photographie-beyrouth/beyrouth-1/ , that is to say /portfolio/parent-category-slug/sub-category-slug/post-title and it should be my-site/portfolio/photographie/beyrouth/beyrouth-1/ that is to say /portfolio/parent-category-slug/sub-category-slug/post-title.

    After that i have installed the plugin Custom Post Type Permalinks, and configure Custom structure for ‘portfolio’: /portfolio/%portfolio_category%/%postname%/ but still not working very good.

    With the Custom Post Type Permalinks plugin, the links works well, the links are /portfolio/category/sub-category/post-title but it takes me on index.php template and not on single-portfolio.php, strange…

    Thank you !

  • The topic ‘Permalinks with CPT and taxonomy category and sub-category’ is closed to new replies.