Output content of post excerpt into Innerblocks within Gutenberg block

I’m building a Gutenberg custom block for pulling details from a custom post type into a Gutenberg block. In my use case, I have a custom post type of Exercises (which is a library of things like Squats, Planks, Burpees etc) and another of ‘Strength and Conditioning Plan’.

The ‘Strength and Conditioning Plan’ post will have this block added to it numerous times, and the reps and sets and notes for each exercise added. The idea is that the user can read their plan and see the exercises they need to do and click into the exercise to get more info about it if they need, or just look at the plan.

So far the block is working well (thanks to help on here!) but I’m having to strip out all the html content from the rendered excerpt and output it into a paragraph block inside my innerblocks.

I’d rather dump all of this into the block instead so it preserves anything we’ve put into the excerpt or the summary automatically created from the page as the excerpt, or maybe even some of the initial post.

What’s the right way of doing this? I can’t find any examples of doing this from a quick Google, but I’m not really sure what to search for! Do I need to some how "convert" the page content back into blocks and append them into the template?

The current set of code can be seen on Github (thanks to Sally CJ for helping out so far!)

EDIT: Some clarifications.

The inner block pulls the title of the "Exercise" post type, along with the featured image. Here is what the innerblocks currently looks like:

const MY_TEMPLATE = [
            [ 'core/heading', { content: heading } ],
            [ 'core/image', { id: mediaID, url: mediaURL, href: exerciseLink, align: "right", sizeSlug: "medium", caption: "Click for instructions", width: 300 } ], //*
            [ 'core/paragraph', { content: strippedContent } ]
        ];

This means the block displays effectively a nice summary of the exercise, including a link to it and a picture. You’ll also get the excerpt which the person who created the ‘Exercise’ will have set up. This might be a summary of all the info in the larger exercise post which we want to be exposed in the ‘Strength and Conditioning Plan’.

Right now, I’m stripping the html from the excerpt and putting it into the paragraph block above. (If you don’t strip it, it breaks because you are putting blocks or html into a paragraph which it doesn’t like!)

I could grab the html from the post and just output it into the Edit/Post output, but:

  • The person setting up the plan might want to tweak it before saving it. Having it as blocks for them makes that nice and easy.
  • If I do output it straight away, the html that comes out isn’t rended by the browser, I’m assuming that brackets etc are turned into & gt; etc.

$299 Affordable Web Design WordPress

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Output content of post excerpt into Innerblocks within Gutenberg block

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.