Ok, so I got it working more or less correctly, although there is still a small problem left, please read on..
Here is what I’ve got:
in fucntions.php
:
Rewrite rules
add_action('init', 'my_rewrite_add_rewrites');
function my_rewrite_add_rewrites(){
add_rewrite_rule(
'^services/([^/]+)/?$',
'index.php?post_type=service&order=ASC&orderby=menu_order',
'top'
);
add_rewrite_rule(
'^services/([^/]+)/page/?([0-9]{1,})/?$',
'index.php?post_type=service&paged=$matches[2]&order=ASC&orderby=menu_order',
'top'
);
}
Redirect rule
add_action("template_redirect", 'my_template_redirect');
function my_template_redirect(){
global $wp_query;
// if Service CPT URL then redirect to archive.php template
if($wp->query_vars["post_type"] == "service"){
include(TEMPLATEPATH . '/archive.php');
exit;
}
}
So now, the URLs of type ‘services/copywriting’ and ‘services/copywriting/page/2’ and so on are working.
However, there is a small problem left. For example, if I’ve got 2 pages of copywriting services in total then the following two URLs are working correctly:
- services/copywriting/
- services/copywriting/page/2
However, the URL with a “non existing page” doesn’t get redirected to 404
template, but still displays archive
template with, obviously, empty content. So how can I make sure that such “non existing” paged URLs will be redirected to 404
template correctly? For example, services/copywriting/page/3
URL when there are only data for 2 pages.
EDIT
I have just also noticed that when a non existing service is referenced as in ‘services/bla-bla’ then the archive
template is also displayed rather than 404
template.
I would hugely appreciate any tips, before I go totally mad.
Thanks.