如何在模式中获取post数据或post Id?
我创建了以下模式文件:
Some Button leading to a post
发布于 2022-12-30 13:47:08
在块主题中,模式将无法访问上下文,如id:https://developer.wordpress.org/block-editor/reference-guides/block-api/block-patterns
必须创建一个自定义块来获取post id,并使用useSelect钩子获取post Id,如下所示:import { useSelect } from "@wordpress/data";
..。内部编辑块功能:
const postId = useSelect(select => select('core/editor').getCurrentPostId());
然后,postId可以在编辑函数中使用,如果它必须用于保存函数,则应该在编辑内部使用useEffect
来存储id,如下所示:
useEffect(() => {
if (postId) {
setAttributes({postId})
}, [postId]);
如果在postId中定义了block.json
属性,则可以在save或render_callback中获取和使用该属性。
发布于 2022-12-26 07:01:54
您可以使用get_the_ID()
。https://developer.wordpress.org/reference/functions/get_这个_身份证/
文件说明:
检索WordPress循环中当前项的ID。
https://wordpress.stackexchange.com/questions/412339
复制相似问题