WordPress 中的动态简码和函数
我在基于数据库条目自动生成短代码方面遇到了一些问题。
我可以获得正常的短代码,即:
function route_sc5() {
return "<div>Route 5</div>";
}
add_shortcode('route 5','route_sc');
并且以下激活它的短代码将是 [route 5]
这有效。但我需要的是为每个数据库条目生成的简码。类似于:
$routes = $wpdb->get_results( $wpdb->prepare("SELECT * FROM wp_routes") );
foreach($routes as $route)
{
function route_sc$route->id () {
return "<div>Route $route->id</div>";
}
add_shortcode('route $route->id','route_sc$route->id');
}
上面只是我希望它如何工作的一个例子。不是字面上我正在使用的代码。我将如何实现这一目标? ): 谢谢。
I am having a bit of an issue with autogenerating shortcodes, based on database entries.
I am able to get a normal shortcode working i.e:
function route_sc5() {
return "<div>Route 5</div>";
}
add_shortcode('route 5','route_sc');
and the following shortcode to activate it would be [route 5]
This works. But what I need is the shortcode to be produced for each database entry. something like:
$routes = $wpdb->get_results( $wpdb->prepare("SELECT * FROM wp_routes") );
foreach($routes as $route)
{
function route_sc$route->id () {
return "<div>Route $route->id</div>";
}
add_shortcode('route $route->id','route_sc$route->id');
}
The above is just an example of how I want it to work. Not literally the code I am using. How would I go about achieving this? ):
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是使用 PHP 5.3 匿名函数 的动态短代码回调的示例:
我有但要问:你能使用短代码参数完成你需要做的事情吗? IE。
[路线编号=3]
。然后,您可以只拥有一个handle_route()
函数和一个[route]
短代码,这可能会简化事情。另外,虽然从技术上讲,您可以在名称中包含一个带有空格的短代码,但我认为这会产生令人困惑的歧义。如果您决定每条路线都需要特定的短代码,我会推荐“route5”或“route-5”而不是“route 5”。
Here's an example of dynamic shortcode callbacks using PHP 5.3 anonymous functions:
I have to ask, though: can you just accomplish what you need to do using shortcode arguments? ie.
[route num=3]
. Then you could just have onehandle_route()
function and one[route]
shortcode, which may simplify things.Also, while technically you can include a shortcode with a space in the name, I think it creates a confusing ambiguity. If you decide you need specific shortcodes for each route, I would recommend "route5" or "route-5" rather than "route 5."
谢谢各位,终于搞定了。这是将来可能需要它的任何人的代码:
短代码位于
[route num="5a"]
Thanks guys, finally got it working. here is the code for any1 who may need it in the future:
with the shortcode at
[route num="5a"]
PHP 中不可能使用动态函数名称。
但你可以尝试评估。
Dynamic function names are not possible in PHP.
But you could try eval.
以不同的方式解决这个问题:短代码可以接受参数。因此,不要使用
[route 5]
,而是使用[route rt="5"]
。这样,您的短代码处理函数就保持通用,并且更改的部分意味着是动态的。这也意味着,如果在页面加载过程中遇到意外的短代码,您可以正确处理它,而不是 WordPress 只是剥离代码并用任何内容替换它。请参阅此处了解更多信息:http://codex.wordpress.org/Shortcode_API
Go about it a different way: Shortcodes can take parameters. So instead of
[route 5]
do[route rt="5"]
. This way your shortcode processing function stays generic and the part that changes is meant to be dynamic. It also means that if an unexpected shortcode is encountered during the page load you can handle it properly instead of WordPress just stripping the code and replacing it with nothing.See here for more info: http://codex.wordpress.org/Shortcode_API