Site logo

Archived topic

Using an Elements Block Content Template to Display Posts on a Page

10 replies · Started by Samuel on December 24, 2021

Viewing posts 1–11 of 11

I know there is a custom loop feature coming to GPP/GBP, and I'm trying to replicate something similar on a site build I'm working on. I have several spots on the site where I need to provide a list of posts according to a custom query. I can easily do the custom query with code, my question is whether there is any way to use call a Block Content Template using code and use it to display the post information so that the design and layout can be done in Elements with the block editor?

For example, I saw this sample code searching for an answer: https://generatepress.com/forums/topic/cpt-query-block-within-a-page-section/#post-1964135. Would something like this work to use the content template within my own custom loops? Or is there a simpler way to do this? Or will it simply not work at all until the new feature is implemented?

I'm very comfortable writing my own php, hooks, etc and woulud love to give the site admin a way to maintain the look and feel of these loops using the block editor if possible.

Hi there,

yes, you could use the method provided in that link.
The only thing is, you need to switch the Element Type in the Block element from Content Template to a Hook - then you can set the Custom Hook to the one inside your query. In my example that was db_custom_post_loop

Great--and using dynamic data sources will work correctly this way?

What is the 'source' ? Does this differ to the post type being output in the loop ?

Meaning can the block pull the title, date, etc out of the current post record if I set it to pull from the data source rather than a fixed value.

Yeah any Dynamic Data you add to the Block Element will be displayed if its available in the queried object.

David,

I'm curious. Is there a way to display the block template directly (for example by it's id) in code instead of assigning the template to a hook? This could make it even more flexible.

Hi there,

This isn't possible at this time, unfortunately. We looked at it before David came up with the hook method.

We'll definitely play with it to see if it's something we can add as a feature :)

Any chance when the custom loop feature comes out there will be a filter or other method to allow completely custom queries? For example, a filter on Query Args before the query is run, etc. I have some situations where I'm sure I'll need to do a custom query to pull the posts but I'm really looking forward to the custom query feature--it will make site maintenance so much easier.

David,

I used your sample code and played with it and it works great. One question what elements/css classes should I add to make a grid render correctly? For example, if I wanted the elements to fill a grid that has two columns (50% each)?

Hi there,

Any chance when the custom loop feature comes out there will be a filter or other method to allow completely custom queries? For example, a filter on Query Args before the query is run, etc. I have some situations where I’m sure I’ll need to do a custom query to pull the posts but I’m really looking forward to the custom query feature–it will make site maintenance so much easier.

That's the goal. But to be specific, I believe this will happen for GenerateBlocks as it is set to be merged with WP Show Posts on Q1 of 2022. :)

This archived topic is closed to new replies.