如果缺少url参数,我将尝试重定向页面。我从中找到以下代码here:
function redirect_to_pending($query) {
if($query->is_main_query() && $query->is_page(26)) {
if(!array_key_exists(\'shazzid\', $query->query_vars) || empty(get_query_var(\'shazzid\'))) {
wp_redirect(get_permalink(1752));
exit;
}
}
}
add_action(\'parse_query\', \'redirect_to_pending\');
它似乎在任何页面上都能很好地工作,除了主页,它会生成以下错误
Notice: Trying to get property \'ID\' of non-object in /wp-includes/class-wp-query.php on line 3956
SO网友:Badger
然而,要解决这个问题,答案似乎是检查查询的对象不为NULL$query->get_queried_object() !== NULL
我不得不删除query\\u var,因为它将主页变成了404。另一个问题。
// redirect if there\'s no job id
function redirect_to_pending($query) {
if($query->get_queried_object() !== NULL && $query->is_page(26)) {
if(!isset($_GET[\'shazzid\'])) {
wp_redirect(get_permalink(1752));
exit;
}
}
}
add_action(\'parse_query\', \'redirect_to_pending\');