I have a custom post type called 'event'. An event has multiple subpages like registration, speakers, contact us, etc. Currently the content is saved as one or more custom fields.
I would like to have an url structure like
events/{event_slug}/registration
events/{event_slug}/speakers
events/{event_slug}/contactus
With each URL displaying the information from each custom field, respectively.
Is there a way, we can achieve this in WordPress?
I have a custom post type called 'event'. An event has multiple subpages like registration, speakers, contact us, etc. Currently the content is saved as one or more custom fields.
I would like to have an url structure like
events/{event_slug}/registration
events/{event_slug}/speakers
events/{event_slug}/contactus
With each URL displaying the information from each custom field, respectively.
Is there a way, we can achieve this in WordPress?
Share Improve this question edited Dec 16, 2019 at 10:03 kero 6,3401 gold badge25 silver badges34 bronze badges asked Dec 5, 2019 at 18:27 gloomgloom 1851 gold badge3 silver badges12 bronze badges 5- 1 You could make the "event" CPT hierarchical and make the details subpages instead of custom fields. – WebElaine Commented Dec 5, 2019 at 19:45
- Yeah..thats true...but alreay I have many events with this setup.. Now I think need to search for a soultion this way only I guess..or do i have a way to switch back to subpages? – gloom Commented Dec 5, 2019 at 20:18
- 1 From my perspective it would be easier to sort out the data and go from there, than live with a suboptimal setup. You could write a script to pull the custom fields out into their own separate "event" cpt posts with the appropriate parent. – WebElaine Commented Dec 5, 2019 at 20:46
- 1 You can auto-create the child pages pulling their content from the custom fields on the parent page, using this type of function: wordpress.stackexchange/a/85832/16 You'd need to modify the $child array to pull in the correct fields via a meta_query. – Michelle Commented Dec 10, 2019 at 21:52
- Do not make subpages. Try using as query variable, ?v=registration or ?v=speakers. Using rewrite rules, you can make it a url. That being done, you can pull custom fields based on query variables by modifying Single event page. – gmatta Commented Dec 17, 2019 at 9:00
2 Answers
Reset to default 2 +25You may want to use this resource. You can create own post types links (and probably relational template structure)
They said you can create domain/{prefix}/{post-type-slug}/{postname}
like URLs with their code (Also you create even custom ones).
One way is to enter an additional query variable and rewrite rule to set it. Meta fields will be displayed based on this new query variable.
add_filter( 'query_vars', 'se353995_query_vars' );
add_action( 'init', 'se353995_sublinks', 20 );
function se353995_query_vars( $vars )
{
$vars[] = "subpage";
return $vars;
}
function se353995_sublinks()
{
$cpt_slug = 'events';
$sublinks = '(registration|speakers|contactus)';
add_rewrite_rule(
"$cpt_slug/([^/]+)(?:/$sublinks)/?$",
'index.php?'.$cpt_slug.'=$matches[1]&post_type='.$cpt_slug.'&subpage=$matches[2]',
'top'
);
}
An example of how to display the page, single-events.php
:
while ( have_posts() ) :
the_post();
// check what event subpage to display
$qv = get_query_var( 'subpage', false );
if ( $qv == 'registration' )
{
// display "registration" subpage
//
$customfield_reg = get_post_meta(get_the_ID(), 'meta_field_name', true) );
}
else if ( $qv == 'speakers' )
{
// display "child page"
//
$customfield_reg = get_post_meta(get_the_ID(), 'other_field_name', true) ;
}
else
{
// default one
//
get_template_part( 'content', 'single' );
}
endwhile; // End of the loop.