php - 在 WordPress 短代码中使用当前帖子 ID

标签 php wordpress shortcode

我正在使用事件管理插件,我想通过短代码显示一些特定的事件详细信息。

我发现了关于这个插件的短代码的非常有用的指南:

https://urjtechhelp.zendesk.com/hc/en-us/articles/115002801594-Embedding-Single-Events-with-the-tribe-event-inline-Shortcode

但我有一个问题。如指南中所述,您必须始终通过 id 短代码属性提供事件的 ID,如下所示:

[tribe_event_inline id="167"]

我想要实现的是短代码始终使用它所在帖子的 ID。

我尝试添加额外的短代码

add_shortcode( 'return_post_id', 'the_dramatist_return_post_id' );

function the_dramatist_return_post_id() {
    return get_the_ID();
}

然后将短代码嵌套在原始代码中,但显然它不会那样工作。

知道如何实现这一目标吗?

预先感谢您的任何想法。

最佳答案

也许有帮助:

add_shortcode( 'return_post_id', 'the_dramatist_return_post_id' );

function the_dramatist_return_post_id() {
    global $post;

    return $post->ID ?? '';
}

关于php - 在 WordPress 短代码中使用当前帖子 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58933766/

相关文章:

wordpress - 如何调试WordPress插件?

jquery - 为来自 jQuery 的伪元素应用 CSS 样式

wordpress - 简码出现在内容的顶部,而不是我需要的地方

wordpress - 如何在模板中使用 wordpress 简码

php - 什么相当于在条令中编写任务的存储库?

php - 将当前屏幕截图并通过php保存为png图片

jquery - Wordpress:菜单小部件实例没有此类方法 'instance'

WordPress 创建空 <p> 标签?

php - 在 Laravel 中与模型工厂播种多对多关系 - 列不能为空错误

php - 页面点击计数器 - 我是否过度使用了数据库?