I was trying to use a custom URL to load a page template for single-events.php
without rewriting the default URL format. I am using filter template_include
to load the template like this:
add_filter('template_include','include_template_for_single_event',99);
function include_template_for_single_event($template)
{
//check_for_single_event() will check if the current url = custom url
if( check_for_single_event() ){
$new_template = locate_template(array('single-events.php','single.php'));
if($new_template)$template = $new_template;
}
return $template;
}
The issue with this is that the contents of the single cpt within the loop is not loading which means there is no data for the single cpt such as get_the_ID()
, have_posts()
etc
Edit:
I do have a default link for a single post of a custom post type
example/nepal/events/visit-year-2020
Now, I want to load this same page template with URL
example/nepal/events/visit-year-2020/2020-01-10.
P.S. client still want the default URL and this new URL with every date available appended to the end.
I was trying to use a custom URL to load a page template for single-events.php
without rewriting the default URL format. I am using filter template_include
to load the template like this:
add_filter('template_include','include_template_for_single_event',99);
function include_template_for_single_event($template)
{
//check_for_single_event() will check if the current url = custom url
if( check_for_single_event() ){
$new_template = locate_template(array('single-events.php','single.php'));
if($new_template)$template = $new_template;
}
return $template;
}
The issue with this is that the contents of the single cpt within the loop is not loading which means there is no data for the single cpt such as get_the_ID()
, have_posts()
etc
Edit:
I do have a default link for a single post of a custom post type
example/nepal/events/visit-year-2020
Now, I want to load this same page template with URL
example/nepal/events/visit-year-2020/2020-01-10.
P.S. client still want the default URL and this new URL with every date available appended to the end.
Share Improve this question edited Jan 10, 2020 at 15:09 nmr 4,5672 gold badges17 silver badges25 bronze badges asked Jan 10, 2020 at 12:21 dbpdbp 1 2 |1 Answer
Reset to default 0I don't know what nepal
is (text prefix only or maybe taxonomy term), but it is not the most important thing, you will correct it if necessary. Rewrite rule like this should resolve the case.
add_action( 'init', 'se356109_events_custom_rule' );
function se356109_events_custom_rule()
{
add_rewrite_rule(
'nepal/events/(.+?)/([0-9\-]+)(:?/page/?([0-9]+))?/?$',
'index.php?events=$matches[1]&paged=$matches[3]',
'top'
);
}
If you need to use URL date (in your example "2020-01-10") somewhere, you need to add a new query variable (with any name, e.g. custom_dt
) and modify rewrite rule.
add_action( 'init', 'se356109_events_custom_rule' );
add_filter( 'query_vars', 'se356109_query_vars' );
function se356109_query_vars( $vars )
{
$vars[] = 'custom_dt';
return $vars;
}
function se356109_events_custom_rule()
{
add_rewrite_rule(
'nepal/events/(.+?)/([0-9\-]+)(:?/page/?([0-9]+))?/?$',
'index.php?events=$matches[1]&paged=$matches[3]&custom_dt=$matches[2]',
'top'
);
}
And that's how you read the value of this new variable:
$dt = get_query_var('custom_dt', false);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744845331a4596799.html
example/nepal/events/visit-year-2020
Now, I want to load this same page template with URLexample/nepal/events/visit-year-2020/2020-01-10
. P.S. client still want the default URL and this new URL with every date available appended to the end. – dbp Commented Jan 10, 2020 at 12:32