Here is my solution to your problem:
in your functions.php
file you should add the following code:
add_action( 'init', 'custom_rewrite_rules' );
function custom_rewrite_rules() {
add_rewrite_rule(
'^(katalog)/([^/]+)/?$',
'index.php?pagename=$matches[1]&bar_variable_name=$matches[2]',
'top'
);
}
add_action( 'init', 'custom_rewrite_tags');
function custom_rewrite_tags() {
add_rewrite_tag( '%bar_variable_name%', '([^/]+)' );
}
Note: In the above code I have name the bar
variable bar_variable_name
. If you like, you can replace the variable name with one that better meets your needs.
Note #2: In case you need to rename the page to something else, then replace the katalog
with the slug of the new page.
After you have done with it, you should go in your WordPress dashboard, in the "Settings / Permalinks" and just save the current settings you use. This way you are going to flush the ReWrite rules of the WordPress.
Then in your theme folder, either create a template page or in your page.php
file and do the following test to check if you are in a page comming with bar
and what is the bar
value:
global $wp_query;
if (
isset( $wp_query->query ) &&
key_exists( 'bar_variable_name', $wp_query->query ) &&
! empty( $wp_query->query['bar_variable_name'] )
) {
$image_path = $wp_query->query['bar_variable_name'];
echo "This is my folder name : " . $image_path;
die();
}
Note: In case you replaced the bar_variable_name
in the previews code block, then update this code by replacing also the bar_variable_name
here too.
If all are going well, when you will visit the katalog
page with an extra segment in the URL you should see the segment string in your page.
ie: if you visit the page /katalog/beers/ the output it should be : This is my folder name : beers
.
If that does what you need, then you can use the $image_path
as variable inside your custom page to display what ever you like.