Display content according to current URL

For this to work, you would have to add a rewrite that rewrites https://example.com/[manufacturer slug]/[brand slug] to index.php with e.g. manufacturer=[manufacturer slug]&brand=[brand slug].
e.g.:

add_filter( 'rewrite_rules_array','jf_insert_rewrite_rules' );
function jf_insert_rewrite_rules( $rules ) {
  $newrules['^(.*)/(.*)/?$'] = 'index.php?manufacturer=$matches[1]&brand=$matches[2]';
  return $newrules + $rules;
}

Remember to goto permalinks page and hit save for the rewrite rules to be updated.

Mind you! It would then not be possible anymore to have a level 1 subpage under a page e.g. /about/contact because this would be replaced with the /[manufacturer slug]/[brand slug] rewrite.
It would probably be better to prefix it with a tag, e.g. /production/manufacturer-1/brand-1 with a rewrite rule like this: ^production/(.*)/(.*)/?$

On top of that, you would need to register the query vars, e.g.:

add_filter('query_vars', 'jf_add_query_vars');
function jf_add_query_vars( $vars ) {
    $vars[] = 'manufacturer'; 
    $vars[] = 'brand'; 
    return $vars;
} 

You would probably want your own template for this pages, so that you can pull from the external api. That could be done by checking the existence of the manufacturer and brand params:

add_action('template_redirect', 'jf_manufacturer_brand_templates');
function jf_manufacturer_brand_templates() {
  if (get_query_var('manufacturer') && get_query_var('brand')) {  
    if (file_exists(get_template_directory() . '/manu_brand.php')) {
      add_filter( 'template_include', function() {
        return get_template_directory() . '/manu_brand.php';
      });
    }
  }
}

You could also have multiple cases here, for if only the manufacturer is available, to show brands per manufacturer. Or even brand only, to show brand detail.

In that template, you can grab the params like this:

var_dump(
    get_query_var('manufacturer'), 
    get_query_var('brand')
);

// hit API with these params and show the result


UPDATE

You had an error in your rewrite rule, the ^production(.*)/ would not match correctly, because my intention was to use production/....

The easiest is to add two more rewrite rules. It could be done in one rewrite rule, but for your understanding of these rules it’s better to make three of them, see code below.

I’ve also updated the function that handles the template selection, so you can see how you could select the correct template. This could also be done in one template, but it’s imho better to have separate templates and separate rules.

Let me know if this helped you ( and remember to goto permalinks page and hit save for the rewrite rules to be updated 😉 ).

add_filter('query_vars', 'jf_add_query_vars');
function jf_add_query_vars( $vars ) {
    $vars[] = 'manufacturer'; 
    $vars[] = 'brand'; 
    $vars[] = 'model'; 
    return $vars;
} 

add_action('template_redirect', 'jf_manufacturer_brand_templates');
function jf_manufacturer_brand_templates() {
  $manufacturer = get_query_var('manufacturer');
  $brand = get_query_var('brand');
  $model = get_query_var('model');
  if ( ! empty($manufacturer) && ! empty($brand) && ! empty($model) ) {  
    if (file_exists(get_template_directory() . '/model-detail.php')) {
      add_filter( 'template_include', function() { return get_template_directory() . '/model-detail.php'; });
    }
  } else if ( ! empty($manufacturer) && ! empty($brand) ) {
    if (file_exists(get_template_directory() . '/brand-detail.php')) {
      add_filter( 'template_include', function() { return get_template_directory() . '/brand-detail.php'; });
    }
  } else if ( ! empty($manufacturer) ) {
    if (file_exists(get_template_directory() . '/manufacturer-detail.php')) {
      add_filter( 'template_include', function() { return get_template_directory() . '/manufacturer-detail.php'; });
    }
  }
}

function jf_insert_rewrite_rules( $rules ) {
  $newrules['^production/(.*)/(.*)/(.*)/?$'] = 'index.php?manufacturer=$matches[1]&brand=$matches[2]&model=$matches[3]';
  $newrules['^production/(.*)/(.*)/?$'] = 'index.php?manufacturer=$matches[1]&brand=$matches[2]';
  $newrules['^production/(.*)/?$'] = 'index.php?manufacturer=$matches[1]';
  return $newrules + $rules;
}
add_filter( 'rewrite_rules_array','jf_insert_rewrite_rules' );

You can see output of working examples here:
https://jos.studioparkers.nl/wpplay/production/manufacturer-1/brand-1/model-1
https://jos.studioparkers.nl/wpplay/production/manufacturer-1/brand-1
https://jos.studioparkers.nl/wpplay/production/manufacturer-1

Leave a Comment