各位代码爱好者,今天咱来聊聊WordPress里一个有点意思的函数:get_post_block_type()。 别看名字平平无奇,它可是WordPress区分文章里那些花里胡哨的“区块”的大侦探。 咱们的目标是,拆解这个函数,看看它是怎么从一堆文本里揪出各种区块类型的。 一、先来认识一下咱们的侦探:get_post_block_type() 这个函数的作用很简单,就是从文章内容中提取出第一个出现的区块的类型。 比如,文章开头第一个区块是个段落,它就返回 core/paragraph。 如果文章里压根就没区块,或者内容为空,它就乖乖地返回 null。 先看下源码的庐山真面目: /** * Retrieves the block type of the first block in the content of a post. * * @since 5.0.0 * * @param WP_Post|int|null $post Optional. Post ID or WP_Post object. Default is global $post. * @return string|nu …
继续阅读“分析 WordPress `get_post_block_type()` 函数的源码:如何从文章中识别出区块类型。”