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

Home Forums Support [Resolved] Using an Elements Block Content Template to Display Posts on a Page

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

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #2058442
    Samuel

    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.

    #2058457
    David
    Staff
    Customer Support

    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

    #2058469
    Samuel

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

    #2058473
    David
    Staff
    Customer Support

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

    #2058474
    Samuel

    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.

    #2058478
    David
    Staff
    Customer Support

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

    #2058688
    Samuel

    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.

    #2060195
    Tom
    Lead Developer
    Lead Developer

    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 🙂

    #2060531
    Samuel

    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.

    #2060960
    Samuel

    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)?

    #2061256
    Elvin
    Staff
    Customer Support

    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. 🙂

    A wise man once said:
    "Have you cleared your cache?"

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.