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 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算