• I would like to know how to prepend the category or taxonomy term to custom post type: example.com/%category%/%post_type%/%post_name%

    I was reading this post: http://www.wpbeginner.com/wp-tutorials/how-to-use-custom-post-types/ And there says (edit: referring to the CMS Press plugin):

    So if you want your custom post type permalink to look like example.com/category/post_type/postname You’ll enter the following under the “Content Type Identifier”: %category%/%identifier%/%postname%

    But when I enter that and update, first get a page without any content. Then I go back, refresh and appear as %categoryidentifierpostname.

    I do not require a solution to do with the plugin but rather a tutorial link to do it by hand in my functions.php file.
    I’ve been looking for a while but the results I get I do not lead to anything.
    Any ideas would be appreciated
    Thanks!

    Ps. Hope I’ve placed this post in the right place.
    And sorry, but yesterday I posted a topic that is inaccessible
    http://wordpress.org/support/topic/prepend-%category-to-custom-post-type?replies=1

Viewing 1 replies (of 1 total)
  • Thread Starter alzuwaga

    (@alzuwaga)

    I did not succeed with CMS Press. but with a little research it seems that I am coming to some approximation. Using the Custom Post Type UI plugin I created:

    – A custom taxonomy (CT) called ‘city’ to which I added the necessary terms (city 1, city 2, city 3)
    – A Custom Post Type (CPT) called ‘accommodation’ and another called ‘gastronomy’. For ‘accommodation’ I put the rewrite rule %city%/accommodation and for ‘gastronomy’ %city%/gastronomy. Both custom post types associated with the custom taxonomy ‘city’.

    Then, based on this article, I placed the following code in functions.php:

    add_filter('post_type_link', 'ciudad_permalink', 10, 3);
    
    function ciudad_permalink($permalink, $post_id, $leavename) {
    	if (strpos($permalink, '%ciudad%') === FALSE) return $permalink;
    
            // Get post
            $post = get_post($post_id);
            if (!$post) return $permalink;
    
            // Get taxonomy terms
            $terms = wp_get_object_terms($post->ID, 'ciudad');
            if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
            else $taxonomy_slug = 'sin-ciudad';
    
    	return str_replace('%ciudad%', $taxonomy_slug, $permalink);
    }

    Permalink is now http://localhost/city-name/post-type/post-name/

    Great!

    Any comment on that?

Viewing 1 replies (of 1 total)
  • The topic ‘Prepend category to custom post type’ is closed to new replies.