如何从另一个页面或模板中获取古腾堡区块内的ACF值?我也在用木材

时间:2019-08-08 作者:Michel Moraes

我试图在我的工作页面上显示一个ACF字段的值,该字段位于自定义post类型项目的块内。

如果我使用如下木材函数获取所有帖子:

$posts = Timber::get_posts(\'post_type=work\');
我得到的结果是:

array (size=1)
  0 => 
    object(Timber\\Post)[1669]
      public \'ImageClass\' => string \'Timber\\Image\' (length=12)
      public \'PostClass\' => string \'Timber\\Post\' (length=11)
      public \'TermClass\' => string \'Timber\\Term\' (length=11)
      public \'object_type\' => string \'post\' (length=4)
      public \'custom\' => 
        array (size=3)
          \'_edit_lock\' => string \'1565259680:1\' (length=12)
          \'_thumbnail_id\' => string \'30\' (length=2)
          \'_wp_old_slug\' => string \'harrods-interior\' (length=16)
      protected \'_content\' => null
      protected \'_permalink\' => null
      protected \'_next\' => 
        array (size=0)
          empty
      protected \'_prev\' => 
        array (size=0)
          empty
      protected \'_css_class\' => null
      public \'id\' => int 29
      public \'ID\' => int 29
      public \'post_author\' => string \'1\' (length=1)
      public \'post_content\' => string \'<!-- wp:acf/hero-case-study {
    "id": "block_5d4ac446baf60",
    "name": "acf\\/hero-case-study",
    "data": {
        "hero_cs_headline": "Lorem ipsum dolor sit amet consectetur adipisicing elit.",
        "_hero_cs_headline": "field_5d4aa12b9cf8b",
        "headline_cs_text_color": "#ffffff",
        "_headline_cs_text_color": "field_5d4ab40954b40",
        "category_cs_text_color": "#3c304c",
        "_category_cs_text_color": "field_5d4ab5205ffa8",
        "hero_cs_bg_image": 42,
        "_hero_cs_bg_\'... (length=6122)
      public \'post_date\' => string \'2019-08-06 16:41:36\' (length=19)
      public \'post_excerpt\' => string \'\' (length=0)
      public \'post_parent\' => int 0
      public \'post_status\' => string \'publish\' (length=7)
      public \'post_title\' => string \'KTURE\' (length=5)
      public \'post_type\' => string \'work\' (length=4)
      public \'slug\' => string \'kture\' (length=5)
      protected \'__type\' => null
      public \'_edit_lock\' => string \'1565259680:1\' (length=12)
      public \'_thumbnail_id\' => string \'30\' (length=2)
      public \'_wp_old_slug\' => string \'harrods-interior\' (length=16)
      public \'post_date_gmt\' => string \'2019-08-06 16:41:36\' (length=19)
      public \'comment_status\' => string \'closed\' (length=6)
      public \'ping_status\' => string \'closed\' (length=6)
      public \'post_password\' => string \'\' (length=0)
      public \'post_name\' => string \'kture\' (length=5)
      public \'to_ping\' => string \'\' (length=0)
      public \'pinged\' => string \'\' (length=0)
      public \'post_modified\' => string \'2019-08-08 08:06:38\' (length=19)
      public \'post_modified_gmt\' => string \'2019-08-08 08:06:38\' (length=19)
      public \'post_content_filtered\' => string \'\' (length=0)
      public \'guid\' => string \'http://simplicitypartners2019.test/?post_type=work&#038;p=29\' (length=60)
      public \'menu_order\' => int 0
      public \'post_mime_type\' => string \'\' (length=0)
      public \'comment_count\' => string \'0\' (length=1)
      public \'filter\' => string \'raw\' (length=3)
      public \'status\' => string \'publish\' (length=7)
如您所见,我需要的是post\\u内容中“hero\\u cs\\u headline”的值。有人能帮我把这个值取下来吗?

如果在twig文件中有另一个解决方案,那也很好。

谢谢

1 个回复
SO网友:cbirdsong

创建ACF块时,需要设置渲染回调:

acf_register_block_type( array(
    \'name\'            => \'example-block\',
    \'title\'           => __( \'Example Block\', \'your-text-domain\' ),
    \'description\'     => __( \'A custom example block.\', \'your-text-domain\' ),
    \'render_callback\' => \'my_acf_block_render_callback\',
    \'category\'        => \'formatting\',
    \'icon\'            => \'admin-comments\',
    \'keywords\'        => array( \'example\' ),
) );
然后在该函数中调用木材函数,就像在页面模板中调用木材函数一样:

function my_acf_block_render_callback( $block, $content = \'\', $is_preview = false ) {
    $context = Timber::context();

    // Store block values.
    $context[\'block\'] = $block;

    // Store field values.
    $context[\'fields\'] = get_fields();

    // Store $is_preview value.
    $context[\'is_preview\'] = $is_preview;

    // Render the block.
    Timber::render( \'block/example-block.twig\', $context );
}
之后,当使用输出时,块将正确渲染{{ post.content }} 或使用标准Wordpress过滤器显示的任何其他内容the_content().

有关更多信息,请访问Timber\'s documentation, 这就是我得到这些片段的地方。

相关推荐