My postname: 127 Hours

My taxonomy name: Actors

I added taxonomy names for post: James Franco

My post link: http:// website.com/127-hours.html

I would like: http:// website.com/127-hours-james-franco.html

I would like add custom taxonomy value in to url.

My functions.php code (for Actor taxonomy)

function add_custom_taxonomies()
    register_taxonomy('actor', 'post', array(
        'hierarchical' => false,
        'labels' => array(
            'name' => _x('Actor', 'taxonomy general name') ,
            'singular_name' => _x('Actor', 'taxonomy singular name') ,
            'search_items' => __('Actor Search') ,
            'all_items' => __('All Actors') ,
            'edit_item' => __('Edit Actor') ,
            'update_item' => __('Update Actor') ,
            'add_new_item' => __('Add New Actor') ,
            'new_item_name' => __('New Actor Name') ,
            'menu_name' => __('Actors') ,
        ) ,
        'rewrite' => array(
            'slug' => 'actor', // This controls the base slug that will display before each term
            'with_front' => false, // Don't display the category base before "/locations/"
            'hierarchical' => false

        ) ,

add_action('init', 'add_custom_taxonomies', 0);

Add Custom Taxonomy Tags to Your WordPress Permalinks

What you wish to do is have your taxonomy available to use within the Permalink structure e.g. /%actor%/%postname%

First you write your taxonomomy

as you have already done, but you require additional fields:

add_action('init', 'add_actor_taxonomy');
function add_actor_taxonomy() {
  if (!is_taxonomy('actor')) {
    register_taxonomy( 'actor', 'post', 
        'hierarchical' => FALSE,
        'label' => __('actor'),
        'public' => TRUE,
        'show_ui' => TRUE,
        'query_var' => 'actor',
        'rewrite' => true, // this adds the tag %actor% to WordPress

Even though the %actor% tag has been added to WordPress it won't work until you tell WordPress how to translate the tag.

// The post_link hook allows us to translate tags for regular post objects
add_filter('post_link', 'actor_permalink', 10, 3);
// The post_type_link hook allows us to translate tags for custom post type objects
add_filter('post_type_link', 'actor_permalink', 10, 3);
function actor_permalink($permalink, $post_id, $leavename) {
  // If the permalink does not contain the %actor% tag, then we don’t need to translate anything.
  if (strpos($permalink, '%actor%') === FALSE) return $permalink;
    // Get post
    $post = get_post($post_id);
    if (!$post) return $permalink;
    // Get the actor terms related to the current post object.
    $terms = wp_get_object_terms($post->ID, 'actor');
    // Retrieve the slug value of the first actor custom taxonomy object linked to the current post.
    if ( !is_wp_error($terms) && !empty($terms) && is_object($terms[0]) )
      $taxonomy_slug = $terms[0]->slug;
    // If no actor terms are retrieved, then replace our actor tag with the value "actor"
      $taxonomy_slug = 'actor';
  // Replace the %actor% tag with our custom taxonomy slug
  return str_replace('%actor%', $taxonomy_slug, $permalink);

With a custom permalink structure like this


Your new post link will look like this


Reference: http://shibashake.com/wordpress-theme/add-custom-taxonomy-tags-to-your-wordpress-permalinks


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.