Support » Plugin: Block Lab » How to access block variables from almost anywhere

  • Resolved dara.kilicoglu

    (@darakilicoglu)


    Hi,

    I want a loop where some fields from posts in certain categories are displayed. However, I can’t access the fields I need with the block_field() calls. Please see the example below.

    <?php query_posts('cat=1,19'); if (have_posts()): while (have_posts()) : the_post(); ?>
    
    <article id="post-<?php the_ID(); ?>" <?php post_class('bk-cover-block d-flex'); ?>
      style="background-image: url(<?php block_field('background-image'); ?>)">
      <h2 class="cover-title"><?php block_field( 'cover-title' ); ?></h2>
      <h3 class="cover-subtitle"><?php block_field( 'cover-subtitle' ); ?></h3>
    </article>
    
    <?php endwhile; endif; wp_reset_query(); ?>

    Is it possible to access block variables outside the block templates?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Luke Carbis

    (@lukecarbis)

    @darakilicoglu wow! Great idea. It’s not currently possible to use block_field outside of a block template, mostly because we’ve never thought of a use case.

    What you’re demonstrating here makes sense though. I do have some questions about it before I create a Github issue (as a feature request).

    How can you be sure that the block containing the background_image and cover_title was used in that post?

    What if the block was added multiple times to the post?

    Could it be that what you’re after here are generic post settings (that live in the “document” part of the sidebar), and not block specific settings?

    How can you be sure that the block containing the background_image and cover_title was used in that post?

    By having some data returned from the function call? 😀

    What if the block was added multiple times to the post?

    Very good question. I haven’t thought about it. In such a case, I wouldn’t expect anything other than an array.

    block_data('bk-cover', 'background-image')
    // string array or a string

    Could it be that what you’re after here are generic post settings (that live in the “document” part of the sidebar), and not block specific settings?

    Generic post settings would work in this example. But It’s hard to say that I will not be wanting to access the post content.

    <?php 
    foreach (parse_blocks(get_the_content()) as $block) {
      if ($block['blockName'] === 'block-lab/bk-cover') {
        // $block['attrs']['background-image']
        // $block['attrs']['cover-title']
        // $block['attrs']['cover-subtitle']
      }
    } 
    ?>

    This is working but not solid at all.

    Thank you @lukecarbis 🙂

    I think this is a super-important request, as it opens up a whole new area of use cases. Similar as @darakilicoglu I’d use block content in loops (e.g. a specific text part, a flag in case a certain block has been used,…), and agree with both proposed solution (function returns data as array).

    My two cents on block vs. generic post settings, it depends if you place the block somewhere in the text (which would be a block then, e.g. something like a custom FAQ, warning text,…) or always at the same place (e.g. a set of alternative post images, introduction text,…). Actually both use cases make sense to me and lead to a 2nd feature request (I didn’t find it on github but forgive me if that has already been placed), do you plan to add document-wide settings as a feature to blocklab (I’d love to use this for certain meta fields, additional featured images with different aspect ratios, or properties such as “sponsored yes/no” with a custom text next to it.

    thanks!
    Jan

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to access block variables from almost anywhere’ is closed to new replies.