我想通过使用另一个已经存在的帖子的永久链接结构以编程方式创建一个简单的HTML页面(不是Wordpress页面)。
。已经存在的职位是www.example.com/this-is-example-post
我想再创建3个页面,即
www.example.com/this-is-example-post/index1
www.example.com/this-is-example-postindex2
www.example.com/this-is-example-post/index3
这些url不一定是真正的页面。我只需要三个不同的页面来存储不同的Facebook元。我正在构建一个插件。请指导。我怎样才能实现它?
我想错了吗?我想得都快疯了。请帮帮我。
------------- 编辑 ---------------
在完成Rahil的建议后,我现在在这里
<?php
add_action( 'init', 'xx_add_endpoint' );
function xx_add_endpoint()
{
add_rewrite_endpoint( 'index', EP_PERMALINK );
}
add_action( 'template_redirect', 'xx_render_endpoint' );
function xx_render_endpoint()
{
if ( ! is_singular() or ! get_query_var( 'index' ) )
{
return;
}
// Do somthing here
exit;
}
add_filter( 'request', 'xx_set_endpoint_var' );
function xx_set_endpoint_var( $vars )
{
isset( $vars['index'] ) and $vars['index'] = true;
return $vars;
}
?>
但我试图了解如何将变量传递给此请求,例如www.example.com/this-is-example-post/index/20/1
, www.example.com/this-is-example-post/index/20/2
, www.example.com/this-is-example-post/index/20/3
等。
注。我打算传递两个值,即/index/a/b
,其中a
, b
是xx_render_endpoint
函数下要发送和处理的两个值。
请导游。
你可以这样做。删除template_redirect
过滤器,你不需要使用这个。相反,使用template_include
过滤器(这是加载备用模板的正确方式)
请参阅我的代码注释。
add_action( 'template_include', 'xx_render_endpoint');
function xx_render_endpoint($default_template)
{
global $wp_query;
// If this isn't singular page or index is not the endpoint
// return default template
if ( ! is_singular() || !isset($wp_query->query_vars['index']) )
{
return $default_template;
}
// our custom template selection variable which default to null
// or you can set a template for just "index" key
// Like: plugin_dir_path(__FILE__) . '/your-template-index.php';
$template_selection = null;
// Why we're exploding index value string into array? As i said in comments
// key => value (index => 1/2). So if you have a url like
// this "post-name/index/1/2", then "1/2" is the value of key "index"
// so we have to separate both values in your case.
$indexes = explode('/', get_query_var('index'));
// Assuming the values "1/2" isset in the url hold the absolute path
// to the template on our $template_selection variable
// Same for the second elseif block instead its just select value "1"
if (isset($indexes[0], $indexes[1])) {
$template_selection = plugin_dir_path(__FILE__) . '/your-template-1-2.php';
} else if (isset($indexes[0])) {
$template_selection = plugin_dir_path(__FILE__) . '/your-template-1.php';
}
// If our $template_selection variable is not null assign the value
// to $default_template which is the required parameter of template_include filter
if (isset($template_selection)) {
$default_template = $template_selection;
}
// Fallback for default template and our template selection
return $default_template;
}
注意:您需要刷新永久链接,并确保您的永久链接结构必须美观,否则它将无法工作