admin管理员组文章数量:1314229
I want to change the HTML output of the latest-posts block (i.e.: wrap the date inside the post link).
I know I can hook into 'blocks.getSaveElement'
and retrieve stuff like the element, blockType and attributes.
But none of this is helpful as this block is rendered in the front-end via php. Is the only way to manipulate the markup to create a custom block and copying the core block files? That seems unintuitive and too much work for swapping two tags. And just writing JS to manipulate the front-end is not an option (might make elements jump around).
Any ideas on how to achieve this in any better way?
I want to change the HTML output of the latest-posts block (i.e.: wrap the date inside the post link).
I know I can hook into 'blocks.getSaveElement'
and retrieve stuff like the element, blockType and attributes.
But none of this is helpful as this block is rendered in the front-end via php. Is the only way to manipulate the markup to create a custom block and copying the core block files? That seems unintuitive and too much work for swapping two tags. And just writing JS to manipulate the front-end is not an option (might make elements jump around).
Any ideas on how to achieve this in any better way?
Share Improve this question asked Nov 25, 2020 at 11:09 UnfuUnfu 431 silver badge4 bronze badges 4 |1 Answer
Reset to default 2The render_block
filter works for this - I just tested on a fresh install. However, you'll have to use the final HTML, so it gets a bit ugly.
The following code will copy the first <a>
tag and wrap the copy around each <time>
tag in the list.
\add_filter('render_block', function($content, $parsed): string {
// skip other blocks
if ($parsed['blockName'] !== 'core/latest-posts') {
return $content;
}
// skip latest posts that don't display the date
if (empty($parsed['attrs']['displayPostDate']) || !$parsed['attrs']['displayPostDate']) {
return $content;
}
$dom = new \DomDocument();
// parse the HTML, the @ is required because DomDocument
// doesn't know about HTML5's <time>
@$dom->loadHTML($content);
// get each individual post
foreach ($dom->getElementsByTagName('li') as $entry) {
$links = $entry->getElementsByTagName('a');
if ($links->count() === 0) {
continue;
}
foreach ($entry->getElementsByTagName('time') as $time) {
// clone the first <a>
$link = $links->item(0)->cloneNode();
// wrap the <time> around that <a>
$time->parentNode->replaceChild($link, $time);
$link->appendChild($time);
}
}
// $dom->saveHTML() returns false on error
$newContent = $dom->saveHTML();
if ($newContent !== false) {
return $newContent;
}
return $content;
}, 10, 2);
However, this does not alter the output inside the editor. Since the block is purely rendered in React nowadays, you'd have to use some of the editor filters for that.
本文标签: filtersHow to change the order of HTML output of a core block
版权声明:本文标题:filters - How to change the order of HTML output of a core block? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741965156a2407504.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
render_block
filter? Did a quick look through the sources, seems like this is the best option. Though you'd have to manually alter the HTML there. – kero Commented Nov 25, 2020 at 11:44getSaveElement
filter. And one does not have access to the inner<li>
elements attributes withrender_block
. – Unfu Commented Nov 25, 2020 at 12:46render_block
(the PHP filter) return the entire HTML? Per the source code it should have the$block_content
which is the result of the render method of the individual block – kero Commented Nov 25, 2020 at 12:55"innerHTML"
attribute is empty. – Unfu Commented Nov 25, 2020 at 13:10