• Resolved Jon Henshaw

    (@jonhenshaw)


    I use the block_field() function to dynamically insert values from a rating block I created into Schema structured data at the end of the single.php template. I created a newsletter block and added it to the post after the rating block. When I did that, it stopped inserting the values from the rating block with block_field(). However, when I moved the newsletter block above the rating block in the post, it worked again.

    It appears that block_field() only inserts values from the last block used in a post.

    (Note: I removed the newsletter block because I want it to appear after the rating block, but you can still see the rating block at the end of the post, and the related schema being populated in the page source.)

    • This topic was modified 3 years, 9 months ago by Jon Henshaw.

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Luke Carbis

    (@lukecarbis)

    @jonhenshaw Could you please post your template code?

    Thread Starter Jon Henshaw

    (@jonhenshaw)

    ratings block

    <div class="ratingsummary">
    	<div class="rating">
    		<div><span><?php block_field( 'rating' ); ?></span></div>
    		<small><?php block_field( 'rating' ); ?> out of 5</small>
    	</div>
    	<div class="pros">
    		<h2>What I liked most</h2>
    		<?php block_field( 'strengths' ); ?>
    	</div>
    	<div class="cons">
    		<h2>Could be better</h2>
    		<?php block_field( 'shortcomings' ); ?>
    	</div>
    </div>

    newsletter block

    <aside class="newsletter">
    	<h2>Never miss an important story</h2>
    	<p>Receive a free weekly <a href="https://coywolf.email">web technology and digital marketing newsletter</a> that's privacy-first and has no ads or sponsors. You can unsubscribe anytime.</p>
    	<form class="js-cm-form" id="subForm" class="js-cm-form" action="https://www.createsend.com/t/subscribeerror?description=" method="post" data-id="5B5E7037DA78A748374AD499497E309ED7BF12DFA4DC856F6DEBF3FFADED8B095E2BDDA4552885D8295813BFC740708594060C41785EEB3CE0A93C4C69D1E6A8">
    		<p><label class="sc-gzVnrw dEVaGV">Email <span class="sc-dnqmqq iFTUZ">*</span></label><input autoComplete="Email" aria-label="Email" id="fieldEmail" placeholder="Enter email address" maxLength="200" name="cm-ojiydky-ojiydky" required type="email" class="js-cm-email-input qa-input-email sc-iwsKbI iMsgpL" /></p>
    		<p><button type="submit" class="js-cm-submit-button sc-jKJlTe iHrMUQ">Subscribe</button></p>
    	</form>
    </aside>
    <script async type="text/javascript" src="https://js.createsend1.com/javascript/copypastesubscribeformlogic.js"></script>

    Snippet of code from single.php template that inserts values from block

    <script defer type="application/ld+json">
    		{
    			"@context": "https://schema.org",
    			"@graph":[
    				{
    					"@context":"https://schema.org",
    					<?php if ( in_category( '1' ) ) : ?>"@type":"Product",<?php elseif ( in_category( '7' ) ) : ?>"@type":"SoftwareApplication",<?php elseif ( in_category( '6' ) ) : ?>"@type":"Book",<?php endif; ?>
    					"name":"<?php block_field( 'name', true ); ?>",
    					"description":"<?php echo get_the_excerpt(); ?>",
    					"image":
    					{
    						"@type": "ImageObject",
    					    "url": "<?php if ( has_post_thumbnail() ) { the_post_thumbnail_url( 'full' ); } else { ?><?php bloginfo('template_directory'); ?>/images/fallback-image.png<?php } ?>",
    					    "height": "600",
    					    "width": "1200"
    					},
    					<?php if ( in_category( '1' ) ) : ?>
    
    					"brand":
    					{
    						"@type":"Thing",
    						"name":"<?php block_field( 'brand', true ); ?>"
    					},
    
    					<?php elseif ( in_category( '7' ) ) : ?>
    
    					"operatingSystem":[<?php block_field( 'os', true ); ?>],
    					"applicationCategory":"<?php block_field( 'category', true ); ?>",					
    
    					<?php elseif ( in_category( '6' ) ) : ?>
    
    					"author":"<?php block_field( 'author', true ); ?>",
    					"copyrightYear":"<?php block_field( 'copyright', true ); ?>",
    					"genre":"<?php block_field( 'genre', true ); ?>",
    					"isbn":"<?php block_field( 'isbn', true ); ?>",
    					"id":"<?php block_field( 'id', true ); ?>",
    Plugin Contributor Luke Carbis

    (@lukecarbis)

    Hi @jonhenshaw –?those top two block templates look good to me. However, block_field doesn’t work inside of single.php, or any theme template (even if you’re inside of the loop, as I presume you are in the example).

    The main reason is because there’s no way for block_field to know which block you’re referring to. Even if there were a parameter for block_field to define the particular block template you’re trying to get (e.g. block_field( 'name', true, 'newsletter' )), it’s still possible you might have inserted multiple copies of that block in your post / page.

    The only way of using block_field() or block_value() is inside the block template, which will be output when you use get_the_content() or the_content() in your single.php template.

    I hope that all makes sense!

    Thread Starter Jon Henshaw

    (@jonhenshaw)

    That’s good to know. It works for me and always has until I recently added the newsletter block after the ratings block. But based on what you’re saying, it shouldn’t be working. If you view the source of https://coywolf.reviews/software/memberful-review/ you’ll see that the Schema is fully populated by it. So I guess I’m utilizing a feature that was never intended to work but does under certain circumstances. I’ll probably keep using what appears to be a hack for the time being. But it looks like what I need to do is migrate the Schema directly into the block. Thanks for your help with this.

    Plugin Contributor Luke Carbis

    (@lukecarbis)

    You’re welcome!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘block_field() only works with last block used in post’ is closed to new replies.