Site icon Hip-Hop Website Design and Development

Customized taxonomy on permalink

I’ve a very long time drawback and look ahead on your assist.
I’ve a customized taxonomy of “metropolis” (it’s no hierarchical like tags).
I simply would love my URLs to appear like this:

/%metropolis%/%class%/%postname%/

So that is what I like to perform.

I’ve made the next:

  1. I’ve created a customized taxonomy – metropolis at first.
  2. I’ve added the code in operate.php
operate add_custom_taxonomies_city() {
    register_taxonomy('metropolis', 'submit', array(
        'hierarchical' => false,
        'labels' => array(
            'identify' => _x( 'Metropolis', 'taxonomy normal identify' ),
            'search_items' =>  __( 'Search cities' ),
            'all_items' => __( 'All cities' ),
            'parent_item' => __( 'Dad or mum Classes' ),
            'parent_item_colon' => __( 'Dad or mum Classes:' ),
            'edit_item' => __( 'Edit metropolis' ), 
            'update_item' => __( 'Replace metropolis' ),
            'add_new_item' => __( 'Add metropolis' ),
            'new_item_name' => __( 'New metropolis' ),
            'menu_name' => __( 'cities' ),
            );    

        ),
    'rewrite' => array(
        'slug' => 'metropolis',
        'with_front' => false,
        'hierarchical' => false
        ),
    ));
}
add_action( 'init', 'add_custom_taxonomies_city', 0 ); 

add_filter('post_link', 'town_permalink', 10, 3);
add_filter('post_type_link', 'town_permalink', 10, 3);

operate town_permalink($permalink, $post_id, $leavename) {
    if (strpos($permalink, '%metropolis%') === FALSE) return $permalink;

// Get submit
    $submit = get_post($post_id);
    if (!$submit) return $permalink;

// Get taxonomy phrases
    $phrases = wp_get_object_terms($post->ID, 'metropolis');  
    if (!is_wp_error($phrases) && !empty($phrases) && is_object($phrases[0])) $taxonomy_slug = $phrases[0]->slug;
    else $taxonomy_slug = 'not-city';

    return str_replace('%metropolis%', $taxonomy_slug, $permalink);
}

The consequence:

This submit has a following hyperlink: http: //mysite.ru/ moscow/purchasing/postname

However I would like the hyperlinks to comprise the child-category.

  1. I’ve tried to resolve the duty with an alternative choice: I’ve added the next code into operate. php
operate addRoutes() {
    add_rewrite_tag('%metropolis%', '(.+?)');
    add_rewrite_rule('(.+?)/(.+?)/([^/]+)(/[0-9]+)?/?$', 'index.php?metropolis=$matches[1]&category_name=$matches[2]&identify=$matches[3]&web page=$matches[4]', 'high');
  add_rewrite_rule('(.?.+?)(/[0-9]+)?/?$', 'index.php?pagename=$matches[1]&web page=$matches[2]', 'high');
    flush_rewrite_rules();
}
add_action('init', 'addRoutes');

In consequence

http://instance.com/metropolis/moscow – Web page not discovered 404 error.
http://instance.com/class/purchasing – Web page not discovered 404 error..
http://instance.com/class/purchasing/grocery store/ – Web page not discovered 404 error..
http://instance.com/moscow/purchasing/grocery store/postname –400 error

It appears the sustem doesn’t perceive %metropolis%
What’s mistaken?
Look ahead on your assist
Can anyone assist me?
Thanks very a lot!


I made a decision to vary the construction of the URLs to:

The code:

//The rewrite guidelines
operate eg_add_query_vars( $query_vars ) {
    $new_vars = array( 'city' );

    return array_merge( $new_vars, $query_vars );
}
add_filter( 'query_vars', 'eg_add_query_vars' );

operate eg_add_rewrite_rules() {
    international $wp_rewrite;

    $new_rules = array(
        'metropolis/(.+?)/class/(.+?)/?$' => 'index.php?metropolis=' . $wp_rewrite->preg_index(1) . '&category_name=' . $wp_rewrite->preg_index(2),
'metropolis/(.+?)/class/(.+?)/(.+?)/?$' => 'index.php?metropolis=' . $wp_rewrite->preg_index(1) . '&category_name=' . $wp_rewrite->preg_index(2) . '&category_name='. $wp_rewrite->preg_index(3),

//'metropolis/(.+?)/class/(.+?)/(.+?)/(.+?)/?$' => 'index.php?metropolis=' . $wp_rewrite->preg_index(1) . '&category_name=' . $wp_rewrite->preg_index(2) . '&category_name='. $wp_rewrite->preg_index(3). '&web page='. $wp_rewrite->preg_index(4),// That is me effort to jot down the rule for posts (would not work)   
        );
    $wp_rewrite->guidelines = $new_rules + $wp_rewrite->guidelines;
}
add_action( 'generate_rewrite_rules', 'eg_add_rewrite_rules' );


//creating the permalink:
operate city_permalink($permalink, $post_id, $leavename) {
    if (strpos($permalink, '%metropolis%') === FALSE) return $permalink;

        // Get submit
        $submit = get_post($post_id);
        if (!$submit) return $permalink;

        // Get taxonomy phrases
        $phrases = wp_get_object_terms($post->ID, 'city');  
        if (!is_wp_error($phrases) && !empty($phrases) && is_object($phrases[0])) $taxonomy_slug = $phrases[0]->slug;
        else $taxonomy_slug = 'not-city';

    return str_replace('%city%', $taxonomy_slug, $permalink);}

    operate wp_town_query( $question ) {
    if( isset( $query->query_vars['city'] ) ):
        if( $metropolis = get_term_by( 'id', $query->query_vars['city'], 'metropolis' ) )
            $query->query_vars['town'] = $city->slug;
    endif;
}
add_action( 'parse_query', 'wp_city_query' );

My settings in Permalink admin web page:
metropolis/%metropolis%/class/%class%/%postname%/

because of this I’ve:
instance.com/metropolis/moscow/class/purchasing/ -works
instance.com/metropolis/moscow/class/purchasing/grocery store/ – works
however nonetheless I can not perceive how one can write the rule for posts:
instance.com/metropolis/moscow/class/purchasing/grocery store/postname – would not.
Pagination not working too.

I will be glad to have any recommendation!

It is work:


'metropolis/(.+?)/class/(.+?)/(.+?)/([^/]+)/?$' => 'index.php?metropolis=' . $wp_rewrite->preg_index(1) . '&category_name=' . $wp_rewrite->preg_index(2). '&category_name=' . $wp_rewrite->preg_index(3). '&identify=' . $wp_rewrite->preg_index(4), 
'metropolis/(.+?)/class/(.+?)/web page/?([0-9]{1,})/?$' => 'index.php?metropolis=' . $wp_rewrite->preg_index(1) . '&category_name=' . $wp_rewrite->preg_index(2). '&paged=' . $wp_rewrite->preg_index(3),
'metropolis/(.+?)/class/(.+?)/?$' => 'index.php?metropolis=' . $wp_rewrite->preg_index(1) . '&category_name=' . $wp_rewrite->preg_index(2),