I want to create most of the website structure based on the external API
Manufactures -> Brands
Here is how the output of API look like
array(2) {
[0]=>
object(stdClass)#448 (6) {
["id"]=>
string(2) "17"
["full_name"]=>
string(11) "manufacture 1"
["label"]=>
string(8) "manufacture 1"
["Description"]=>
string[25] "Description manufacture 1"
["url"]=>
string(15) "/manufacture-1/"
["brands"]=>
array(2) {
[0]=>
object(stdClass)#442 (27) {
["id"]=>
string(3) "151"
["full_name"]=>
string(34) "brand 1"
["label"]=>
string(19) "brand 1"
["Description"]=>
string[25] "Description Brand 1"
["url"]=>
string(23) "/manufacture-1/brand-1/"
}
[1]=>
object(stdClass)#142 (27) {
["id"]=>
string(3) "152"
["full_name"]=>
string(34) "brand 2"
["label"]=>
string(19) "brand 2"
["Description"]=>
string[25] "Description Brand 2"
["url"]=>
string(23) "/manufacture-1/brand-2/"
}
}
}
[1]=>
object(stdClass)#2448 (6) {
["id"]=>
string(2) "19"
["full_name"]=>
string(11) "manufacture 2"
["label"]=>
string(8) "manufacture 2"
["Description"]=>
string[25] "Description manufacture 2"
["url"]=>
string(15) "/manufacture-2/"
["brands"]=>
array(2) {
[0]=>
object(stdClass)#2442 (27) {
["id"]=>
string(3) "351"
["full_name"]=>
string(34) "brand 3"
["label"]=>
string(19) "brand 3"
["Description"]=>
string[25] "Description Brand 3"
["url"]=>
string(23) "/manufacture-2/brand-3/"
}
[1]=>
object(stdClass)#3442 (27) {
["id"]=>
string(3) "352"
["full_name"]=>
string(34) "brand 4"
["label"]=>
string(19) "brand 4"
["Description"]=>
string[25] "Description Brand 4"
["url"]=>
string(23) "/manufacture-2/brand-4/"
}
}
}
}
So based on above api I want to create link and display data according For eg:
On home page I will display 2 manufacturers
if user click on manufacturer 1
he goes to page https:/example.com/manufacture-1
and then we display two brands - Brand 1 and Brand 2 and description of Manufacturer 1
If I click on Brand 1 then he goes to page https:/example.com/manufacture-1/brand-1
and then we display data accordingle
SO how can I do this i.e If I am on page https:/example.com/manufacture-1/brand-1 , I detect what is url and display content accordingly
Please point me in the right direction , how to proceed on this
EDIT ->
Based on your answer , @Jos Faber I added code but with a bit edit to accomodate 3rd quary var (Manufactures -> Brands -> Models)
following is the code
add_filter( 'rewrite_rules_array','jf_insert_rewrite_rules' );
function jf_insert_rewrite_rules( $rules ) {
$newrules['^production(.*)/(.*)/(.*)/?$'] = 'index.php?manufacturer=$matches[1]&brand=$matches[2]&model=$matches[3]';
return $newrules + $rules;
}
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() {
if (get_query_var('manufacturer') || get_query_var('brand') || get_query_var('model')) {
if (file_exists(get_template_directory() . '/manu_brand.php')) {
add_filter( 'template_include', function() {
return get_template_directory() . '/manu_brand.php';
});
}
}
}
code of manu_brands.php is
echo "Manufacturer=".get_query_var('manufacturer').'<br>';
echo "brand=". get_query_var('brand').'<br>';
echo "model=". get_query_var('model').'<br>';
var_dump(
get_query_var('manufacturer'),
get_query_var('brand'),
get_query_var('model')
);
https://www.servers-hosting.com/production/manufacture-1/brand-1/model-1/ ---- Works perfect
https://www.servers-hosting.com/production/manufacture-1/brand-1/ --- See page it returns wrong value for wrong query var
https://www.servers-hosting.com/production/manufacture-1/ ---- 404 error
Also for template redirect I had change to || instead of &&
Can you please help me out with this please