• 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' );

    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>
    
    

    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-name/sub-category-slug/post-title and it should be my-site/portfolio/photographie/beyrouth/beyrouth-1/ that is to say /portfolio/parent-category-name/sub-parent-category-name/post-title, so in fact just sub-parent-category-name should be display instead of sub-parent-category-slug-name.

    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 good.

    So i do not know if WordPress does not support hierarchical taxonomies with the exact structure you’re trying to achieve.

    In this situation, achieving such a permalink structure might require customizing the rewrite rules at a deeper level, which can be quite complex. But I don’t think this type of site is so unusual, on the contrary there are plenty of configurations where you have to have a CPT with categories and subcategories?

    Thank you !

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Are you wanting to use a term’s name (not slug) in the permalink? That can cause problems because names can be composed of characters that are illegal to have in URLs. Permalinks should always be composed of slugs, not names.

    You may have a hierarchical taxonomy in a permalink structure. The default structure for custom post types doesn’t include taxonomies, but you can customize things to incorporate taxonomies in the structure. You need to filter the permalink creation so links are structured as desired. Then you need a rewrite rule to tell WP how to parse such a structure. I assume Custom Post Type Permalinks would be doing this for you. If it’s not working as expected, I recommend asking for help through its dedicated support channel.

    Thread Starter fredericbdr

    (@fredericbdr)

    Thanks, i want to use slug it will be ok but it does not work.

    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…

    Moderator bcworkz

    (@bcworkz)

    Maybe because there are category terms involved, WP thinks it’s an archive request? Incorrect all the same. It may be because of how its rewrite rule is generated. Because this is related to a plugin, it might be preferable to patch things up instead of trying to correct the root cause. For example, through the “pre_get_posts” action hook, check the values of is_archive and is_single properties. They might not be correct and could be corrected in this callback.

    You’d need to somehow confirm the request is for a single portfolio from other query vars besides things like is_single() which could be suspect.

    If all else fails, you could force any desired template be used through hooks like “template_include” and “template_redirect”.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Permalinks with custom post type and taxonomy category’ is closed to new replies.