douyueju2912 2012-11-21 09:37
浏览 47
已采纳

如何委托短代码功能,以便使用WordPress中的Pods Framework过滤当前用户创建的pod项目

I want to list some pod items on front end. To do this I am normally using pods shortcode. To filter the data I use where attribute inside pods shortcode. For example:

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

I want to filter pod items by the author.ID. The user should see the pod items that are created by himself. It is possible to access the current user by wordpress api global $current_user;. Also it is possible to get it by using magic tags of Pods {@user.ID}.

But the problem is I cannot use php code or magic tags inside shortcode such as this:

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

I am looking for a way to overcome this problem.

I thought maybe I can write a new shortcode function where I can get the current user's id and then call the pods shortcode function such as this:

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

Do you think that this is the right way to solve this problem? And do you know how to put $user_id into $atts and delegate the rest to the actual pods shortcode function?

  • 写回答

1条回答 默认 最新

  • doutuohan6606 2012-11-21 17:22
    关注

    In your pods_by_current_user function, try doing:

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

    and then return the normal Pods shortcode:

    return pods_shortcode( $atts );

    This will let you use any of the normal [pods] shortcode attributes as usual, but will set the 'where' to automatically limit to the current user ID. I'm sure you can make this more complex, but that will do what you're after.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 eclipse连接sap后代码跑出来空白
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案