Skip to content
pinegrow.com
Docs menuWordPress

Displaying up-to-date information with dynamic blocks

How to create a Recent blog posts block?

On this page

The content of a Regular JavaScript block gets set when we save the post in the Block editor.

Dynamic PHP Blocks, on the other hand, are rendered every time they are displayed. And because they are run on the server-side and coded in PHP they can use any WordPress function to query posts, site information, current user and more.

Create a dynamic block

Let’s create a dynamic block that will display the three most recent blog posts.

We will start with the HTML layout for the section:

Recent blog posts section with three post cards showing images, titles, dates and excerpts, and a See all posts button.

We define the block by adding Block action to the <section> element and set the id to recentposts, title to Recent posts and icon to images-alt.

Pinegrow section settings for the recent posts block with Unique Id recentposts, Title Recent posts and a description filled in.

Show posts with custom query

Select the first post column div and in the WordPress panel add Show Posts action with Loop type “Custom query”, Order by “Created date - Down”, Posts per page “3” and Paginate results “No”.

Show Posts - Smart panel with Loop type set to custom query, Order by Created date and Posts per page set to 3.

Show Posts action will read the posts from the WordPress database and repeat the element for each post.

Select both <a> elements and add the Post link action:

Post Link - Smart panel with the post title link selected in the Pinegrow canvas of a blog post card.

On the <img> element we add the Post Featured Image action and set the image size to “medium”:

Post Featured Image - Smart panel with Size set to medium, with the image of the first recent blog post selected.

Post Title action goes to the <h4> element:

Post Title - Smart panel with the title of the first recent blog post selected in the canvas.

Next, we add Post Date action to the <p> element with the date:

Post Date - Smart panel with the date of the first recent blog post selected in the canvas.

And the Post Excerpt action to the <p> with the post excerpt:

Post Excerpt - Smart panel with the Only display if excerpt is set option unchecked, and the excerpt paragraph selected.

What to do with the other placeholder posts in our HTML project?

The Show posts is already set up on the first post and we don’t want to export the other two placeholder posts to the theme. To do that we add Don’t Export action from the Global Smart Actions section of the WordPress panel to both posts:

Recent blog posts with a column selected and a Don't Export - Smart action, so the placeholder cards are not exported.

Editable title and button

Let’s make the section title and button editable as well.

Select the Recent blog posts <h3> and add Block Attributes action:

Block Attributes - Smart panel with Block Attribute 1 set to attribute id title and Use as Content for the Recent blog posts heading.

On the button, add Block Attributes action and make both the link and the label editable:

Block Attributes - Smart panel with button_link used as a Link and button_label used as Content, for the See all posts button.

One advantage of dynamic blocks is that we can freely mix block attributes and WordPress actions.

See it in action

Export the project. Because the block contains actions such as Show Posts that runs on the server-side, Pinegrow automatically exported the block as Dynamic PHP block.

Reload the Block editor and add the Recent posts block to the page.

Recent posts block titled The latest waves with a Jump in button, and the block sidebar showing its title and button link fields.

We can notice a slight delay when displaying the block, due to its content being rendered on the server.

Dynamic blocks are rendered each time they are displayed, so our block will always show the most recent posts.

The post selector control

How about if we want to hand pick the list of posts that is displayed in the block?

That’s easy! We just need to change the Loop type to “block” on the Show Posts action, and fill in the Attribute id, title and description:

Show Posts - Smart panel with Loop type set to block and Attribute id posts, above a preview of the Recent blog posts cards.

Using the block query type adds the Post selector control to the sidebar. It lets us hand pick the posts we want to display.

The latest waves section with a Hand pick posts callout, and the Recent posts block settings listing Monster waves and Going through tubes.

Use the up, down and remove controls to manipulate the list of selected posts:

Recent posts block sidebar with Selected posts listing Monster waves and Going through tubes, and a Select a post button.

When to use dynamic blocks?

Dynamic blocks are very powerful because we can use any of the 200+ WordPress actions and include custom PHP code. So, why not just use dynamic blocks all the time?

Dynamic blocks have three drawbacks though:

  • The delay during editing due to server side rendering.
  • They can slow down the page display, if we use too many of them on the page.
  • Text attributes can not be edited on the block, only in the sidebar.

So, we should use Dynamic blocks only when needed and stick to regular JavaScript blocks in other cases.

Note, most non-native block solutions such as ACF PRO Block support only dynamic blocks because they are easier to implement. Pinegrow is unique in supporting easy creation of both JavaScript and PHP blocks.

Next step

Next, we will learn how to add custom CSS styling to our blocks.