[Resolved] Applying Block Element – Content Template Styles to CPTs in a Custom Shortcode

Home Forums Support [Resolved] Applying Block Element – Content Template Styles to CPTs in a Custom Shortcode

Home Forums Support Applying Block Element – Content Template Styles to CPTs in a Custom Shortcode

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1824729
    Nome

    I have a shortcode for displaying a custom post type (‘event’) within different parts of my site. I also have a Content Template Block Element for handling the formatting of the event CPT. The formatting works great on the single and archive pages, as you’d expect, but I can’t figure out how to get it to apply to the CPT when I access it through my own WP_Query loop.

    Here is a simplified version of what that code looks like now:

    
    function popular_events_shortcode( $atts ) {
    // ...
      if ( $events->have_posts() ) {
        ob_start();
        while ( $events->have_posts() ) {
          $events->the_post();
          generate_do_template_part( 'single' );
        }
        return ob_get_clean();
      }
    }
    
    #1824837
    Elvin
    Staff
    Customer Support

    Hi Nome,

    You’ll need a compatible template for your custom post type for the Block Element – Content template to be able to apply to it.

    Check David’s brief explanation here – https://generatepress.com/forums/topic/how-to-design-custom-post-type/#post-1682069

    #1825694
    Nome

    Hi Elvin,

    Thanks for your quick response, however that doesn’t quite answer my question. I understand how to create custom post templates in my child theme, but I am trying to avoid any implementation of layout or design in the theme so my client can edit it all from within WP Admin. It seems like with the new Content Block Elements this should be possible.

    It doesn’t seem like I need a compatible template to apply the Content Template. It works just fine on the single and archive pages for my custom post type already.

    It looks like within GeneratePress, the basic templates first check for generate_has_default_loop(), so I suppose my question is if Content Templates skip over the default loop entirely, and handle their templating elsewhere. Or is there some other function or flag I need to set to ensure the element is applied?

    Thanks.

    #1825757
    Nome

    Hello again,

    I spent a while looking through the code. It looks like there isn’t a way to achieve what I want; it seems that each block element loads once per page, and is controlled by a boolean whether it applies or not. As far as I can tell, there isn’t a way to have multiple content templates active on the same page, e.g. if there were multiple content types on the front page: news posts, an event widget, etc.

    Thanks for your help, I will just go the old fashioned template route.

    It would be a cool feature to see in a future release of GP. It would be great to have different dynamic content templates that can be applied to certain post types, categories, etc, even when they are on the same page. It doesn’t seem easily possible the way the code is currently structured, so I won’t hold my breath, but I know I can’t be the only one who has multiple content and post types that all benefit from slightly different styling, and tend to co-exist.

    #1826003
    Elvin
    Staff
    Customer Support

    I spent a while looking through the code. It looks like there isn’t a way to achieve what I want; it seems that each block element loads once per page, and is controlled by a boolean whether it applies or not. As far as I can tell, there isn’t a way to have multiple content templates active on the same page, e.g. if there were multiple content types on the front page: news posts, an event widget, etc.

    Yes this is true. Because content template targets the same thing so if you have 2, only one will apply.

    This is quite tricky to implement has the Block element has no way (yet?) of determining which post loop to apply things on. It assumes that the page set on the display rule has a standard loop of the theme, targets it and applies the content template.

    This is quite complex. I’ll tag Tom on this and see what he has to say. 😀

    #1827156
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    That’s definitely the hard thing here – knowing which loop to apply to, and how to target that particular loop. I think this is where the re-built WP Show Posts will come into play, so you can design your own custom loops and place them anywhere you want. GP/GPP handles the theme output vs custom loop output.

    #1830253
    Nome

    Thank you, Tom.

    I’m excited to see the future of WP Show Posts. I think for those of us that have a background in theme and plugin development, that would make the GP ecosystem fully dev friendly. This is my first time using GP for a client website, rather than custom coding my own theme, and for the most part I really like it as a base.

    I’ll keep an eye out for that release.

    #1830375
    Tom
    Lead Developer
    Lead Developer

    I agree, it’s the missing piece right now. We’re working on it 🙂

    Thanks!

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