【发布时间】:2018-03-19 07:57:27
【问题描述】:
是否可以确定当前的 Wordpress url 是否为端点页面?
- 漂亮的固定链接正在使用中
- 可以在 wp_head 之后迟到,但越早越好
仅使用:
global $wp_query;
isset( $wp_query->query_vars['articles'] )
不工作正确。我不能接受这个作为答案。请不要有链接。
似乎更关注这个问题。上面的示例returns true 在 slug 为 'articles' 的存档页面上,或者当自定义帖子类型使用此 slug 或名为 'articles' 的单个页面或另一个使用 'articles' 的唯一 slug 时,这不是重写规则端点"指向目标”。
注意:如果有人有 set_query_var('articles', $articles) 将其传递到模板范围,它也是 returns true。 (否则,下面提供的函数 确实有效,直到我偶然发现了模板文件插件)。
如果在set_query_var() 中设置变量,则该问题在其他示例中仍然存在,例如“位置”或“地址”。
遍历所有 post_name 的 db 只会返回帖子和页面 slug 以从检查部分“删除”。自定义存档的重写规则不在数据库中。
对于 AMP 页面,端点可能位于网址的开头或结尾,因此定位条件检查也不起作用。
到目前为止,唯一的帮助是在某个地方找到了这个函数(剪切):
function is_endpoint(){
if(!get_option('permalink_structure')) return false;
global $wp;
if(!$wp->request) return false;
$parts = explode('/', $wp->request);
if(empty($parts)) return false;
// https://codex.wordpress.org/Reserved_Terms
$reserved = array(
'revision',
'nav_menu_item',
'custom_css',
'customize_changeset',
'action',
'attachment',
'attachment_id',
.. AND ALL OTHER FROM CODEX DOCS ...
'year'
);
//global $wp_query (clashes with added stuff);
global $wp_the_query;
foreach($parts as $maybe_endpoint){
if(!$maybe_endpoint || !trim($maybe_endpoint)) continue;
$maybe_endpoint = trim($maybe_endpoint);
if(post_type_exists($maybe_endpoint)) continue;
if(in_array($maybe_endpoint, $reserved)) continue;
/** if(is_slug($maybe_endpoint)) continue;**/ // MAYBE SOMETHING LIKE THIS?
/* Final conditional, all other possibilities removed: */
if(isset($wp_the_query->query_vars[$maybe_endpoint])) {
return true;
break;
}
}
return false;
}
但它似乎还必须检查重写规则,以排除存档/注册的分类法蛞蝓。
对此有任何可能的意见吗?
【问题讨论】:
-
这真的很有趣,可以解决
标签: wordpress conditional endpoint