i'm trying to find a way to add some custom field in the post template that lies under the core/query of wordpress. The idea is to add in this post-template a div with a data-id element containing the value of my custom field (that is already added in every custom post in the back end) Right now, i'm able to catch the meta query of my posts inside a custom block that inherits the query loop. It works in the edit.js part, in the editor, my loop append the meta field (i integrate it in a data-id label) correctly for each post. But i'm not able to do it on the save.js for the front end as it only register one meta field value. Maybe i'm missing something in the way the query loop works. If somebody has some idea or clue in order for me to move on. Thank you!
Here's my block.json
"$schema": ".json",
"apiVersion": 3,
"name": "my-block/test",
"version": "0.1.0",
"title": "Test",
"ancestor": [ "core/query" ],
"parent": ["core/post-template"],
"usesContext": [
"queryId",
"query",
"displayLayout",
"templateSlug",
"previewPostType",
"enhancedPagination",
"postType",
"postId"
],
my edit.js
export default function Edit({ attributes, setAttributes, context }) {
const { cardId, postId } = attributes;
const meta = useEntityProp('postType', context.postType, 'meta', context.postId);
meta ? setAttributes({ cardId: meta[0].my_cpt }) : "";
setAttributes({ postId: context.postId })
const blockProps = useBlockProps({
"data-id": cardId,
"className": postId
});
return (
<div {...blockProps}>
<InnerBlocks />
</div>
);
}
my save.js
export default function Save({ attributes }) {
const { cardId, postId } = attributes;
const blockProps = useBlockProps.save({
"data-id": cardId
});
return (
<div { ...blockProps }>
<InnerBlocks.Content />
</div>
);
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744841568a4596585.html
评论列表(0条)