Get specific Blocks from Post

时间:2021-06-11 作者:LinguiniCoder

我试图将单个帖子块(按类型)加载到单独的div中,我尝试过过滤标签,但结果是有一堆被丢弃的div,总体上很混乱,而且不可预测。

我想知道是否有一种方法可以检索文章中的段落块?

1 个回复
最合适的回答,由SO网友:bosco 整理而成

PHP端可以通过post_contentthe parse_blocks() function 这将为您提供一个表示每个块的关联数组。JS端以以下形式提供了类似的功能the wp.blocks.parse() function@wordpress/blocks 包裹为了便于说明,以下是var_dump() 默认输出的;示例页:

array(9) {
  [0] => array(5) {
    \'blockName\' => string(14) "core/paragraph"
    \'attrs\' => array(0) {}
    \'innerBlocks\' => array(0) {}
    \'innerHTML\' => string(278) "
<p>This is an example page. It\'s different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this:</p>"
    \'innerContent\' => array(1) {
      [0] => string(278) "
<p>This is an example page. It\'s different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this:</p>"
    }
  }
  [1] => array(5) {
    \'blockName\' => NULL
    \'attrs\' => array(0) {}
    \'innerBlocks\' => array(0) {}
    \'innerHTML\' => string(2) "

"
    \'innerContent\' => array(1) {
      [0] => string(2) "

"
    }
  }
  [2] => array(5) {
    \'blockName\' => string(10) "core/quote"
    \'attrs\' => array(0) {}
    \'innerBlocks\' => array(0) {}
    \'innerHTML\' => string(260) "
<blockquote class="wp-block-quote"><p>Hi there! I\'m a bike messenger by day, aspiring actor by night, and this is my website. I live in Los Angeles, have a great dog named Jack, and I like pi&#241;a coladas. (And gettin\' caught in the rain.)</p></blockquote>"
    \'innerContent\' => array(1) {
      [0] => string(260) "
<blockquote class="wp-block-quote"><p>Hi there! I\'m a bike messenger by day, aspiring actor by night, and this is my website. I live in Los Angeles, have a great dog named Jack, and I like pi&#241;a coladas. (And gettin\' caught in the rain.)</p></blockquote>"
    }
  }
  [3] => array(5) {
    \'blockName\' => NULL
    \'attrs\' => array(0) {}
    \'innerBlocks\' =>
    array(0) {}
    \'innerHTML\' => string(2) "

"
    \'innerContent\' => array(1) {
      [0] => string(2) "

"
    }
  }
  [4] => array(5) {
    \'blockName\' => string(14) "core/paragraph"
    \'attrs\' => array(0) {}
    \'innerBlocks\' => array(0) {}
    \'innerHTML\' => string(35) "<p>...or something like this:</p>"
    \'innerContent\' => array(1) {
      [0] => string(35) "<p>...or something like this:</p>"
    }
  }
  [5] => array(5) {
    \'blockName\' => NULL
    \'attrs\' => array(0) {}
    \'innerBlocks\' => array(0) {}
    \'innerHTML\' => string(2) "

"
    \'innerContent\' => array(1) {
      [0] => string(2) "

"
    }
  }
  [6] => array(5) {
    \'blockName\' => string(10) "core/quote"
    \'attrs\' => array(0) {}
    \'innerBlocks\' => array(0) {}
    \'innerHTML\' => string(288) "
<blockquote class="wp-block-quote"><p>The XYZ Doohickey Company was founded in 1971, and has been providing quality doohickeys to the public ever since. Located in Gotham City, XYZ employs over 2,000 people and does all kinds of awesome things for the Gotham community.</p></blockquote>"
    \'innerContent\' => array(1) {
      [0] => string(288) "
<blockquote class="wp-block-quote"><p>The XYZ Doohickey Company was founded in 1971, and has been providing quality doohickeys to the public ever since. Located in Gotham City, XYZ employs over 2,000 people and does all kinds of awesome things for the Gotham community.</p></blockquote>"
    }
  }
  [7] => array(5) {
    \'blockName\' => NULL
    \'attrs\' => array(0) {}
    \'innerBlocks\' => array(0) {}
    \'innerHTML\' => string(2) "

"
    \'innerContent\' => array(1) {
      [0] => string(2) "

"
    }
  }
  [8] => array(5) {
    \'blockName\' => string(14) "core/paragraph"
    \'attrs\' => array(0) {}
    \'innerBlocks\' => array(0) {}
    \'innerHTML\' => string(180) "
<p>As a new WordPress user, you should go to <a href="http://localhost:8888/wp-admin/">your dashboard</a> to delete this page and create new pages for your content. Have fun!</p>"
    \'innerContent\' => array(1) {
      [0] => string(180) "
<p>As a new WordPress user, you should go to <a href="http://localhost:8888/wp-admin/">your dashboard</a> to delete this page and create new pages for your content. Have fun!</p>"
    }
  }
}
当为某个块类型分析此数组时,如core/paragraph, 应该注意的是,您应该遍历每个块\'innerBlocks 数组,以寻址嵌套块,例如列或组中的嵌套块:

/**
 * Processes a block list output from the parse_blocks() function
 * and returns a flat list of block arrays matching the specified
 * type.
 * 
 * @param array  $blocks Output from parse_blocks().
 * @param string $type The block type handle to search for.
 * @return array An array of all block arrays with the specified type.
 **/
function wpse390419_find_blocks( $blocks, $type ) {
  $matches = [];

  foreach( $blocks as $block ) {
    if( $block[\'blockName\'] === $type )
      $matches[] = $block;

    if( count( $block[\'innerBlocks\'] ) ) {
      $matches = array_merge(
        $matches,
        wpse390419_find_blocks( $block[\'innerBlocks\'], $type )
      );
    }
  }

  return $matches;
}
$paragraph_blocks = wpse390419_find_blocks(
  parse_blocks( get_the_content() ),
  \'core/paragraph\'
);
然而,如果您试图从内容中解析块,以便在特定位置显示它们,则在某种程度上破坏了块编辑器的最大优势之一,即所见即所得界面。更好的解决方案是实施Block PatternBlock Template 以限制块的放置。

相关推荐