簡體   English   中英

如何委派給簡碼功能以過濾當前用戶使用Wordpress中的Pods Framework創建的Pod項

[英]How to delegate to a shortcode function in order to filter pod items created by the current user using Pods Framework in Wordpress

我想在前端列出一些吊艙物品。 為此,我通常使用pods簡碼。 為了過濾數據,我在pods短代碼中使用where屬性。 例如:

[pods name="salon" where="author.ID = '2'"]
    <li>{@name}</li>
[/pods]

我想按author.ID過濾pod項。 用戶應該看到自己創建的pod項目。 可以通過wordpress api global $current_user;訪問當前用戶global $current_user; 也可以通過使用Pods {@user.ID}魔術標簽來獲取它。

但是問題是我不能在短代碼中使用php代碼或魔術標簽,例如:

[pods name="salon" where="author.ID = '{@user.ID}'"]

我正在尋找一種克服此問題的方法。

我以為也許可以寫一個新的shortcode函數,從中獲取當前用戶的ID,然后調用pods shortcode函數,如下所示:

function pods_by_current_user($atts) {
    $current_user = wp_get_current_user();
    $user_id = $current_user->ID;
    // put it into 'where' attribute and delegate to the "pods" function

您是否認為這是解決此問題的正確方法? 並且您知道如何將$user_id放入$atts並將其余的委托給實際的pods shortcode函數嗎?

在您的pods_by_current_user函數中,嘗試執行以下操作:

$atts[ 'where' ] = 'author.ID = ' . (int) $user_id;

然后返回正常的Pods簡碼:

return pods_shortcode( $atts );

這將使您像往常一樣使用任何常規的[pods]簡碼屬性,但會將“ where”設置為自動限制為當前用戶ID。 我敢肯定,您可以使其變得更復雜,但這將滿足您的要求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM