I have a simple task, change the url from vikatorr.ru/portfolio-view/aisuma/ to the url of this type vikatorr.ru/aisuma/ in arbitrary types of recording. However, I could not find in the documentation how to set it up using PHP. Tell me how to do this for the entire post type. As a result, 404 http://vikatorr.ru/banksalen/

/* Portfolio */ function my_post_type_portfolio() { register_post_type( 'portfolio', array( 'label' => theme_locals("portfolio"), 'singular_label' => theme_locals("portfolio"), '_builtin' => false, 'public' => true, 'show_ui' => true, 'show_in_nav_menus' => true, 'hierarchical' => true, 'capability_type' => 'page', 'menu_icon' => get_template_directory_uri() . '/includes/images/icon_portfolio.png', 'rewrite' => FALSE, 'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'comments') ) ); register_taxonomy('portfolio_category', 'portfolio', array('hierarchical' => true, 'label' => theme_locals("categories"), 'singular_name' => theme_locals("category"), "rewrite" => true, "query_var" => true)); register_taxonomy('portfolio_tag', 'portfolio', array('hierarchical' => false, 'label' => theme_locals("tags"), 'singular_name' => theme_locals("tag"), 'rewrite' => true, 'query_var' => true)); } add_action('init', 'my_post_type_portfolio'); global $wp_rewrite; $projects_structure = '/%portfolio%/'; $wp_rewrite->add_rewrite_tag("%portfolio%", '([^/]+)', "\portfolio="); $wp_rewrite->add_permastruct('portfolio', $projects_structure, True); 

    2 answers 2

    Try through .htaccess

     RewriteEngine On RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?portfolio-view=$1 RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?portfolio-view=$1 
    • It does not seem, I think all the same it is worth doing with the help of the topic. - Basil Jimmy
    • @VasilyukDmitry give me your .htaccess - KYRAN
    • # BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase/RewriteRule ^ index \ .php $ - [L] RewriteCond% {REQUEST_FILENAME} /index.php [L] </ IfModule> # END WordPress - Basil Jimmy
    • @VasilyukDmitry updated the answer - KYRAN 1:51

    In general, I understood for a long time, but successfully. All this for the Cherry Framework.

     add_filter('pre_get_posts','exclude_post'); add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' ); add_filter( 'rewrite_rules_array','portfolio_rewrite_rules' ); add_filter( 'query_vars','my_insert_query_vars' ); add_action( 'wp_loaded','my_flush_rules' ); // Сбиваем правила flush_rules(), если наших еще нет в списке (операция ресурсоемкая, поэтому не стоит ее делать каждый раз) function my_flush_rules(){ $rules = get_option( 'rewrite_rules' ); // достаточно проверить, есть ли в списке хотя бы одно из наших правил if ( ! isset( $rules['meetings/([^/]+)(/[0-9]+)?/?$'] ) ) { global $wp_rewrite; $wp_rewrite->flush_rules(); } } // добавляем наши правила function my_insert_rewrite_rules( $rules ) { $newrules = array(); //$newrules['banksalen/bryllyp/?$']='index.php?portfolio=banksalenbryllyp'; //$newrules['(aisuma|cafe-bagle|tavern-vertshus|banksalen|den-gode-nabo|grenaderen-vertshus|gronn-pepper|frati-catering|sushi-bar|gjest-baardsen|havfruen-selskapslokaler|mormors-stue|selskapslokale|festsalen|bari-cafe-og-bar|havfruen|bryggen-asian|frati)/?$']='index.php?portfolio=$matches[1]'; $newrules['forum/([^/]*)/([^/]+)/([0-9]+)/?$'] = 'index.php?topic=$matches[2]&paged=$matches[3]'; $newrules['forum/([^/]*)/([0-9]+)/?$'] = 'index.php?forum=$matches[1]&paged=$matches[2]'; $newrules['forum/([^/]*)/(.*)/([0-9]+)/?$'] = 'index.php?topic=$matches[2]&paged=$matches[3]'; $newrules['forum/([^/]*)/(.*)/?$'] = 'index.php?topic=$matches[2]'; $newrules['forum/([^/]*)/?$'] = 'index.php?forum=$matches[1]'; $newrules['(library|books|articles|tools|projects|diagrams)/([0-9]{1,})/?$'] = 'index.php?category_name=$matches[1]&paged=$matches[2]'; $newrules['(library|books|articles|tools|projects|diagrams)/?$'] = 'index.php?category_name=$matches[1]'; $newrules['([0-9]{1,})/?$'] = 'index.php?&paged=$matches[1]'; $newrules['meetings/([^/]+)(/[0-9]+)?/?$'] = 'index.php?name=$matches[1]&page=$matches[2]&post_type=event'; return $newrules + $rules; // так оно добавляет наши правила к уже существующим, а еще мы можем тупо убрать все и вернуть только свой список, сделав return $newrules; } function portfolio_rewrite_rules( $rules ) { $newrules = array(); $newrules['banksalen/banksalen-i-trondheim/?$']='index.php?portfolio=banksalen-i-trondheim'; $newrules['banksalen/konferansesenter-i-trondheim/?$']='index.php?portfolio=konferansesenter-i-trondheim'; $newrules['banksalen/skreddersy-ditt-selskap/?$'] = 'index.php?portfolio=skreddersy-ditt-selskap'; $newrules['banksalen/forretningsmiddag/?$']='index.php?portfolio=forretningsmiddag'; $newrules['banksalen/julebord-banksalen-festlokaler-julebord-for-firma-trondheim/?$']='index.php?portfolio=julebord-banksalen-festlokaler-julebord-for-firma-trondheim'; $newrules['banksalen/forretningsmiddag/?$']='index.php?portfolio=forretningsmiddag'; $newrules['banksalen/bryllyp/?$']='index.php?portfolio=bryllyp'; $newrules['('.portfolio_rerule().')'.'/?$']='index.php?portfolio=$matches[1]'; return $newrules + $rules; }