Building With

GenerateBlocks

Useful tips for building advanced blocks.

Query Loop – Related Posts

At the end of a single blog post, many sites show related content. With Query Loop, you can now dynamically pull in related articles based on categories.

1

Go to Appearance > Elements.

Appearance-Elements-v2

2

Click on Add New Element.

Elements Add New Element v2

3

Select Block for the Element Type and click on Create.

Element Type Block v1

4

You can name the Element anything you want. I named it “Related Posts”.

After naming the element, click on ‘+‘ sign to add a new block.

Related Posts Add Block v1

5

Scroll down to GenerateBlocks and click on the Query Loop block to add it.

Add Query Loop

6

Query Loop will give you multiple layout options. Select the one that best suits your blog.

Query Loop Layout Options

7

Leave the Select post type as Posts and you can modify the Posts per page to a number of your preference.

Query Loop Add Parameter

8

Select Taxonomies from the scroll-down menu.

Query Loop Taxonomies v1

9

Then select Categories for Taxonomies and select Current post terms for Select Terms.

Query-Loop-Current-Post-Terms-v1

10

Then select Categories for Taxonomies and select Current post terms for Select Terms.

Afterwards, click on Add Parameter because you will be adding a second parameter to exclude the current post.

Query Loop Current Post Terms v2

11

This time select Exclude posts from the drop-down menu.

The reason you would want to exclude the current posts is that you wouldn’t want the reader to see the current post they are already on as an option.

Query Loop Exclude Post v1

12

Scroll to the bottom of the page and you will see the Display Rules option. This allows you to select where you would like to see the related posts published.

Select Post and All Posts from the secondary menu.

Display Rules Post v1

13

On the far right-hand side, take a look near the top. You’ll find the option to switch from Block to Element. Click on Element.

Scroll down to Element Type and select Post Navigation.

Then go to Quick Hook Select and select After content container.

The Hook Name should be after_do_template_part.

Update the Priority to 1 and enable the Disable default post navigation by toggling the button.

Element Post Navigation v1

Final Results

Click on Publish, and you are done!

Now, if you go scroll down to any of your posts, near the bottom you’ll notice related posts show up.

Related-Posts-v1-Reduced

Leave a comment