Passing the current page ID to a template

I’ve made two custom post types, one of which (style-section) is the parent of the other (style-rule), and has a relationship field that connects the two. On the parent posts I want to loop through the related child content.

If I use an actual post ID (206 in this example, below) this works. But I want to dynamically get the post ID of the current post, and I haven’t managed to find a way to do this.

<Loop type=style-rule custom_field="style-section" custom_field_value="206">
  <h2><Field title /></h2>
  <div><Field content /></div>
</Loop>

Managed to get this working using this:

<Set name=current_id>
  <Field id />
</Set>

<Loop type=style-rule custom_field="style-section" custom_field_value="{Get name=current_id}">
  <h2><Field title /></h2>
  <div><Field content /></div>
</Loop>

Hey @juliush, welcome to the forum! Glad to see that you’ve managed to figure it out yourself :smile:

While the code you’ve shared achieves what you’re trying to do, I thought I’d chime in with a tip to improve the performance of your L&L markup. In your case, there’s no need to use Set and Get to define and use a custom variable because you could just as easily reference the Field id in your loop’s query parameters like this:

<Loop type=style-rule custom_field="style-section" custom_field_value="{Field id}">
  <h2><Field title /></h2>
  <div><Field content /></div>
</Loop>

You might find the this section of the “Getting started with L&L terminology and syntax” article useful as further reading to better understand how to use pass dynamic data to a tag attribute.

Great, thanks, that looks much cleaner. I was trying to get something like that to work but couldn’t.

1 Like