admin管理员组文章数量:1122846
I have created a custom post type and I want to create blocks that display its values. To start with, I created a placeholder that gets replaced with the correct values using PHP.
Everything works as expected on the frontend. However, I also want to display the correct values in the admin (block editor), but I'm struggling to get it to work.
I need possibly the postId but i can't get it.
This is an example of one of my blocks:
custom-blocks.js:
registerBlockType('custom-plugin/custom-name', {
title: 'Name',
category: 'common',
edit: function(props) {
const blockProps = useBlockProps();
const postTitle = 'how to get this???';
return createElement(
'p',
blockProps,
postTitle
);
},
save: function() {
const blockProps = useBlockProps.save();
return createElement(
'p',
blockProps,
'[[ Gruppe/Titel ]]'
);
}
});
custom-posttype.php:
register_block_type('custom-plugin/custom-name', array(
'editor_script' => 'custom-blocks',
'render_callback' => array($this, 'render_titel_block'),
));
public function render_titel_block($args = [], $content = '') {
if(is_array($args)) $args = (object) $args;
$block_id = '[[ Gruppe/Titel ]]';
$post = isset($args->post) ? $args->post : (isset($args->post_id) ? get_post($args->post_id) : $GLOBALS['post']);
if (!$post instanceof WP_Post || $post->post_type !== $this->post_type) {
return 'Error';
}
$titel = get_the_title($post->ID) ?: '-';
$titel = esc_html($titel);
$html = $content;
if (strpos($html, $block_id) !== false) {
$html = str_replace($block_id, $titel, $html);
}
return $html;
}
The custom block is nested in core/query -> custom/card-layout.
I have created a custom post type and I want to create blocks that display its values. To start with, I created a placeholder that gets replaced with the correct values using PHP.
Everything works as expected on the frontend. However, I also want to display the correct values in the admin (block editor), but I'm struggling to get it to work.
I need possibly the postId but i can't get it.
This is an example of one of my blocks:
custom-blocks.js:
registerBlockType('custom-plugin/custom-name', {
title: 'Name',
category: 'common',
edit: function(props) {
const blockProps = useBlockProps();
const postTitle = 'how to get this???';
return createElement(
'p',
blockProps,
postTitle
);
},
save: function() {
const blockProps = useBlockProps.save();
return createElement(
'p',
blockProps,
'[[ Gruppe/Titel ]]'
);
}
});
custom-posttype.php:
register_block_type('custom-plugin/custom-name', array(
'editor_script' => 'custom-blocks',
'render_callback' => array($this, 'render_titel_block'),
));
public function render_titel_block($args = [], $content = '') {
if(is_array($args)) $args = (object) $args;
$block_id = '[[ Gruppe/Titel ]]';
$post = isset($args->post) ? $args->post : (isset($args->post_id) ? get_post($args->post_id) : $GLOBALS['post']);
if (!$post instanceof WP_Post || $post->post_type !== $this->post_type) {
return 'Error';
}
$titel = get_the_title($post->ID) ?: '-';
$titel = esc_html($titel);
$html = $content;
if (strpos($html, $block_id) !== false) {
$html = str_replace($block_id, $titel, $html);
}
return $html;
}
The custom block is nested in core/query -> custom/card-layout.
Share Improve this question edited Sep 19, 2024 at 12:42 Ghiath Sardast asked Sep 19, 2024 at 12:40 Ghiath SardastGhiath Sardast 11 bronze badge1 Answer
Reset to default 0We can utilize the useSelect hook
from the @wordpress/data
package to display the post title
in custom block
within the WordPress block editor. useSelect hook
allows us to fetch the post data using the current post ID within the block editor. Here’s the updated code of custom-blocks.js
to achieve this.
import { registerBlockType } from '@wordpress/blocks';
import { createElement } from '@wordpress/element';
import { useBlockProps } from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
registerBlockType('custom-plugin/custom-name', {
title: 'Name',
category: 'common',
edit: function(props) {
const blockProps = useBlockProps();
// Here we are useing useSelect hook to get the current post ID and title.
const postId = props.clientId.split('-')[0]; // This is to get the post ID from clientId.
const postTitle = useSelect(select => {
return select('core/editor').getEditedPostAttribute('title');
}, [postId]);
return createElement(
'p',
blockProps,
postTitle || 'No title available'
);
},
save: function() {
const blockProps = useBlockProps.save();
return createElement(
'p',
blockProps,
'[[ Gruppe/Titel ]]'
);
}
});
本文标签: phpHow to get the post title inside a custom block in a loop blockjs
版权声明:本文标题:php - How to get the post title inside a custom block in a loop? block.js 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736290069a1928436.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论