一种方法是输入一个额外的查询变量,然后重写规则进行设置。元字段将基于此新查询变量显示。
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\'
);
}
如何显示页面的示例,
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.