最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - URL rewrite rule

matteradmin4PV0评论

I have taxonomy product_brand and product_cat

I have a link:

/product_brand/brand_name/?attributes[product_cat]=product_cat_id

How to convert to pretty url?

/product_brand/brand_name/product_cat_name

  function custom_rewrite_rules() {
        add_rewrite_rule('^product_brand/(.*)/(.*)?', 'index.php?&attributes[product_cat]=$matches[1]', 'top');
  }
    add_action('init', 'custom_rewrite_rules');

I have taxonomy product_brand and product_cat

I have a link:

/product_brand/brand_name/?attributes[product_cat]=product_cat_id

How to convert to pretty url?

/product_brand/brand_name/product_cat_name

  function custom_rewrite_rules() {
        add_rewrite_rule('^product_brand/(.*)/(.*)?', 'index.php?&attributes[product_cat]=$matches[1]', 'top');
  }
    add_action('init', 'custom_rewrite_rules');
Share Improve this question asked Mar 19, 2019 at 15:06 AlexyAlexy 1013 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0
    function custom_rewrite_rules() {
            add_rewrite_rule( '^product_brand/([^/]+)/page/([0-9]{1,})/?', 'index.php?product_brand=$matches[1]&page=$matches[2]', 'top' );
            add_rewrite_rule( '^product_brand/([^/]+)/([^/]+)/page/([0-9]{1,})/?', 'index.php?product_brand=$matches[1]&product_cat=$matches[2]&page=$matches[3]', 'top' );
            add_rewrite_rule('^product_brand/([^/]+)/([^/]+)?', 'index.php?product_brand=$matches[1]&product_cat=$matches[2]', 'top');

            add_filter( 'query_vars', function( $vars ){
                $vars[] = 'product_brand';
                $vars[] = 'product_cat';
                return $vars;
            } );
     }
     add_action('init', 'custom_rewrite_rules');

and pagination

add_action( 'pre_get_posts', function() {
 $page = get_query_var('page');
 $query->set( 'paged', $page );
 return $query;
} ); 

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far