Macro question about reusing code for different loop types

I have a Loop to show a single post

<Loop type="dogs" id="{Get local=postid}">
  ( lots of cool code here)

But sometimes, I want to use the same cool code in a different loop

<Loop type="dogs" paged=8>
  ( the same cool code here)

Is there any way to reuse so I only need one copy of my cool code ?
I’ve toyed with

<If check="{Get local=pid}" >
  <Loop type="dogs" id="{Get local=pid}">
<If check="{Get local=pgnum}" >
  <Loop type="dogs" paged=8>

But the whole loop needs to be inside the If which means I need to duplicate the code.

I also tried moving the cool code to a separate template without success

<Loop type="dogs" id="{Get local=postid}">
  [template id=loopless-bastard]

Any ideas?

Hey Richard, you’re on the right track! I think what you’re looking for is the Template tag. That tag allows you to nest loops inside other loops to your heart’s content and is probably the most useful (and most underused) way to build sites with L&L since it allows you to more easily follow DRY development principles.

I’m also not sure about the greater context of the markup you’re writing, but I thought I’d share another tip just in case. I saw some of your recent posts (like this one) where you’re manually writing out a custom variable in your shortcode to define the current “post id” so that you can reference it later in your template. Just in case, I thought I’d mention that when it comes to passing data about the current post (the post on which the template or shortcode is placed) to the template, it’s unnecessary to pass information like the current post ID to the template since L&L is already using the current post as its context.

In other words, keeping with your “dogs” site theme, let’s say you had a template that just contained <h2>This dog's name is <Field title /></h2> and nothing else. If you used the shortcode to reference that L&L template on one of your “dogs” posts, the template would already know that you wanted to display fields from the current post so there wouldn’t be a need to feed data like the post id to the template. However, if you were on the home page (for example) and wanted to use the template shortcode to display fields from a specific dogs post, then passing that info as a variable would certainly be a good way to go. Not sure what your use case is but I just thought I’d mention it just in case that helps you avoid having to write extra markup!

Thanks @benjamin

I try to avoid head slap moments, but this is one. I should have known I can call a template from within another template.

Until now, I have only been using the template tag in its shortcode version to call templates from html, often from inside a BB posts module, custom layout. That’s also why I define the current “post id” because when you call a template from inside a loop not driven by L&L, it doesn’t seem to know the current post id.

interestingly (for anyone else following this) the solution to this post also involved passing the post Id as a variable

In the instance you referenced, I showed my standard language when it wasn’t necessary.

Ultimately, I’d like to get rid of BB Posts modules completely!

1 Like

Interesting, I’ll have to see if I can replicate this so that I can send that to the devs. Unless you’d be willing to share access to your site so that we could look into it? If you’re willing, you could send the credentials to and I’ll see what we can do.

It seems that on a fresh WP install, L&L works as you’d expect and knows the context its in for each post without needing to pass any data to the template using local variables. I just put together this BB themer layout for blog posts and I customized the post layout to add a little template shortcode. You can see that for each post, it grabs the category terms associated with the individual posts as you’d expect.

Weird that this isn’t happening on your site. Let me know if you can share any other info about what might be causing this behaviour.

Can you share the content of template 1655?

Yeah sure, this is all I’ve got inside that template:

<strong>Here are this post's category terms:
  <Loop taxonomy=category post=current>
    <span style="color:red;"><Field title /></span>

Edit: just for fun, I tried adding a little <Field title /> tag to the beginning of the template (before I open the taxonomy loop) and that seems to make my taxonomy loop display data from the same post every time. Don’t think that’s what that’s supposed to do haha! I’ll make a note of this for the devs to see what they can learn from this. Thanks for prompting me to test that a bit more!

Thank you for mentioning that. I was going to do some tests, but seems like there is an underlying problem.

1 Like