最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Add meta field in corepost-template wordpress - Stack Overflow

programmeradmin4浏览0评论

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> 
    );
}
发布评论

评论列表(0)

  1. 暂无评论