“get_current_user_id()”與數字的嚴格比較失敗

伊丹·卡塔拉格

我有一個技術問題,我希望對此有更多的見解而不是解決方案。

我將自定義函數掛鉤到 WordPress functions.php 中的“template_include”過濾器,如下所示,以便在其他人無法觀看的頁面上工作。

function template_redirect( $template ) {
    if ( $template === locate_template('single-ressource.php') && get_current_user_id() === 11 ) { 
        return $new_template = locate_template( array( 'single.php' ) );
    }
    return $template;
};
add_filter( 'template_include', 'template_redirect', 99 );

因此,如果任何未登錄到我的帳戶的人轉到“ressource”自定義帖子類型頁面,他們會使用標準的 single.php 佈局而不是 single-ressource.php 佈局看到它。

問題是,它不能按原樣工作。我必須11將嚴格比較中整數更改'11'字符串以使其 工作 請參閱編輯),如下所示。

function template_redirect( $template ) {
    if ( $template === locate_template('single-ressource.php') && get_current_user_id() === '11' ) { 
        return $new_template = locate_template( array( 'single.php' ) );
    }
    return $template;
};
add_filter( 'template_include', 'template_redirect', 99 );

我去看了get_current_user_id()函數的官方文檔,似乎他們使用類型轉換來返回整數或0.

此外,當我var_dump(get_current_user_id())在前端執行 a時,它返回int(11).

任何人都知道為什麼 第二個代碼有效 請參閱編輯)而不是第一個?

編輯

正如@Bazaim 指出的那樣,我只是對所涉及的邏輯感到困惑。

對於第二個代碼,我想隱藏的實際“single-ressource.php”並未對任何人隱藏,因為傳遞的條件背後的邏輯存在缺陷。我只重定向了一個user_id等於一個字符串的用戶,這個字符串'11'不是一個,因為user_id是整數。

下面的代碼完美地工作。

function template_redirect( $template ) {
    if ( $template === locate_template('single-ressource.php') && get_current_user_id() !== 11 ) { 
        return $new_template = locate_template( array( 'single.php' ) );
    }
    return $template;
};
add_filter( 'template_include', 'template_redirect', 99 );
巴扎姆

你的 user_id 是 11 嗎?

你需要看到single.php

  • $template'single-ressource.php':對
  • get_current_user_id()11:錯了,你不希望 id 是11

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

get_current_user_id()返回零0

get_current_user_id()不能与wp_login挂钩一起使用-WordPress

使用get_current_user_id()的简单IF语句出现问题

对于Wordpress-如何在javascript中调用get_current_user_id

使用 Ruby 解決插入排序時,整數與 nil 的比較失敗(ArgumentError)

REST嚴格驗證與否?

为什么在woocommerce_login_redirect挂钩中使用get_current_user_id()返回0

將 forloop 變量與數字進行比較

調用泛型函數時的嚴格類型參數

如何在 JavaScript 中使數組數據類型(對象)嚴格相等(===)

比較列表中的數字

數組值與多維數組 ruby 的比較

TypeScript 如何為子構造函數使用更嚴格的類型?

嚴格模式。從引用的函數中提取內部定義的變量

pandas 數據框迭代作為列表的單元格值並將每個元素與其他單元格進行比較

'string | 類型的參數 undefined' 不能分配給類型為 'string' 的參數。打字稿“嚴格”

當與單引號內的數字進行比較時,MySQL“=”的行為類似於“like”運算符

使用嚴格類型獲取點擊元素的 ID

指針與整數C++比較的問題

Devcon 禁用端口與數字中繼

在 C# 中將 list<int> 與數字相乘

比較 JSON 模式中的數字

為什麼 typescript 在實現抽象函數時會忽略嚴格的 null 檢查?

DAX - PowerBi - 從數組中嚴格匹配子集字符串並為其賦值

使用遞歸計數方法將整數與某些數字相除

為什麼 tr 與 é 一起失敗?

將 owner_id 與 request.user.id 進行比較以在 django 中進行身份驗證是否安全?

R - 將數據幀中的值與聚合數據幀進行比較

將 ENUM 中的數值與參數進行比較