Loop type=post category=?

Hi all - trying to migrate over from CCS and have been at it most of the night.

Can’t seem to get the last bit of a quick code I’m trying though - I can’t seem to nail down the variable I’m looking for. I’m trying to write a template that I can use on many different pages that will use whatever the value is after the first slash of the domain name:

Eg. https://www.domainname.com/thisIsWhatIWantToUse

<Loop type=post orderby=date order=desc category=thisIsWhatIWantToUse exclude_category=54>
    <li>
      <a href="{Field url}"><Field title /></a>
    </li>
  </Loop>

but I’m getting nowhere. Anyone wanna flex their savvy and help me out?

Thanks in advance.

Hi turnerbuds, I’m not 100% sure what you’re trying to achieve, but you can try this code:

<Loop>
  <Set name=thisIsWhatIWantToUse><Format case=pascal><Field slug/></Format></Set>
</Loop>
<Get thisIsWhatIWantToUse></Get>

You can use this to print to the page to make sure it’s actually what you want to use. You can remove the if it causes problems, it’s just to capitalize first letters of words which will help if category needs to be for example Blog instead of blog. If all works well you can put it in your category by writing,

"{Get thisIsWhatIWantToUse}"

Hope this helps:)

1 Like

It’s a bit unclear exactly what you’re trying to achieve, but if I understand correctly, you’re trying to use the first part of your URL to define which category of posts should be displayed. Is that right?

Jhoanne is right that <Field slug /> might get you what you’re looking for, although if you’re actually on the page https://www.domainname.com/thisIsWhatIWantToUse and want to display that part of the URL, then the Route tag might give you better control over that. So the way I’d do this would be

<Loop type=post orderby=date order=desc category="{Route part=1}" exclude_category=54>
  <li>
    <a href="{Field url}"><Field title /></a>
  </li>
</Loop>

Also minor note for @Sunskie, I just realized that the current documentation doesn’t seem to mention this, but there’s no need to create a <Loop> with no attributes to get information from the current page/post. If you want to display a field from the current post, you can just write <Field field_name /> by itself in the template.

1 Like

Thank both!
@Sunskie - sorry if I wasn’t clear, but yeah… I’m trying to take part of the URL after the slash for whatever page I’m on and use THAT as part of the Loop query.

@benjamin - right you are - that’s what I was after. I tried your code and it immediately worked! I tried it on a page where the content after the slash doesn’t match the category I want, so I have some changes to make but thank you kindly - I don’t think I would have ever found whatever ‘route’ is.

Now I’m going to play with how to get that Category to just display on the front-end without the need for a loop, and then the big one - loops for posts with more than one category!

Thanks again!

2 Likes

Ok - well that didn’t work out…

I am trying to find documentation on the syntax to use if I want to display only posts that have more than one category. Not “include”, but AND.

I’ve tried a number of things, but nothing seems to work:

Doesn’t work:

<If loop exists type=post orderby=date order=desc category="{Route part=1}" and_category=extra exclude=current>

Doesn’t work:

<If loop exists type=post orderby=date order=desc category="{Route part=1}",extra exclude=current>

Doesn’t work: (actually works like an OR statement)

<If loop exists type=post orderby=date order=desc category="{Route part=1}" category=extra exclude=current>

I’ve worked out a few other things and they’ve all worked great but this one is driving me a little batty… any suggestions?

Hi Turnerbuds:)

I tried to find the posts with more than 1 category, by looping through the taxonomy terms of Category on the current post. Then I use the count of that loop to determine if a post has 2 or more categories.

<Loop type=post>
  <Loop type=taxonomy_term taxonomy=Category post=current count=2>
   </Loop>
   <If previous_total more_than_or_equal value="2"> 
     <Field title/>
   </If>
</Loop>

This was printing out the titles of the posts that had more than 1 category. I hope this helps, and nothing weird happens when you replace:

<Loop type=post>

with

<Loop type=post orderby=date order=desc category="{Route part=1}" exclude_category=54>

:slight_smile:

1 Like

Thanks @Sunskie but that didn’t seem to work out - I got too many results - essentially any time a post had two categories assigned instead of exactly the two I need: “{Route part=1}” and 54.

I’m a little surprised though - is there not a command or syntax to use in the first loop to say “List only posts that are part of both Category X and Category Y”?

eyo:)

I am not sure if I can still follow what your asking for but if you want a List or an array to your post type then maybe you can use this:

<List name=somename>
  <Item>CategoryX</Item>
  <Item>CategoryY</Item>
</List>

<Loop type=post category="{Get list=somename}" >
  <Field title/>
</Loop>
1 Like

Thanks for trying but this doesn’t work either.

I’m sorry I’m not able to express what I’m after - I’m really surprised it’s not more common.

Is it rare for posts to belong to more than one category? Is it rare to want to pull up a list of posts that belong to not just 1, but 2 specifically?

I think this sentence threw me off the same way it threw off Jhoanne. I interpreted it as meaning that you were looking to display any post that has more than one category, not that you were looking for posts that had two specific categories associated with it. The latter is simple, the former requires the more complex syntax that Jhoanne came up with.

There are a ton of query parameters you can use to adjust a post loop. The category query parameter is great when you just want to display posts in certain categories, like category=firstcategory,secondcategory. Or if you wanted one of those categories to be filled in dynamically, the syntax would be category="{Route part=1},extra". But these just use “or” logic as you’d noticed.

If you want more control than what the simple category parameter gives you, you’ll want to use the taxonomy, taxonomy_compare, terms, and taxonomy_relation parameters. Those give you a ton more control when you want to display posts that match specific categories using certain logic. You’ll probably be able to figure out how those work from looking at the docs linked above, but let me know if you get stuck.

Minor note for @Sunskie, if you want to add multiple values to an attribute, there’s no need to create a list variable and render it in the attribute value. You can just write it out with a comma-separated list in the value, just like you would in HTML. So something like category="CategoryX,CategoryY" in your example. Edit: I just realized that arrays aren’t a thing in HTML but they’re a thing in other languages like javascript. I should probably add an explanation about what arrays are to the docs so people who only know HTML are made aware that that’s a thing.

2 Likes

Thanks for the reply - and yes I’d tried:

<Loop type=post orderby=date order=desc category="{Route part=1},extras">

And it gave me OR results (everything from both categories, not the posts that had both).

I’ll keep working - but gotta say… I find it so odd this functionality isn’t more commonly used, and not specifically addressed in any post, guide, how-to, website, or other forum question :slight_smile:

Yeah, the current docs are a little sparse on how-tos and examples. I’m actually currently working on some revised docs that should launch within the next week (I expect you’ll get an email about it) that are going to include a lot more examples. As far as forum posts, I found this thread and this one that mention displaying posts based on multiple category/taxonomy terms. The parameters I mentioned/linked to in my earlier post will definitely allow you to achieve what you’re trying to do.

I’m going to add some examples to the new docs soon to demonstrate how you’d use these parameters together so here’s one I just put together. Hopefully this will help you understand how all those parameters work together to allow you to write complex rules for which taxonomy terms should/shouldn’t have their posts displayed. The example below would show posts in an “event” post type that have the category term “seminar” applied and also don’t have either the terms “workshop” or “training” applied:

<Loop type=event taxonomy=category terms=seminar taxonomy_relation=and taxonomy_2=category taxonomy_compare_2=not terms_2=workshop,training>
2 Likes

Oh, thanks Bens:) I was unaware of that syntax, that will really help me out in the future! I love this forum, you know? I’m learning alot from other people specially from you Bens:):slight_smile:

2 Likes

… aaaaaaaand THAT’s what I needed, @benjamin!

This now does what I want - listing ONLY posts that have BOTH categories: “{Route part=1}” AND “extras”:

<If loop exists type=post orderby=date order=desc taxonomy=category terms="{Route part=1}" taxonomy_relation=and taxonomy_2=category taxonomy_compare=and terms_2=extras>
  <Loop>
    <li>
      <a href="{Field url}" target="_blank" rel="noopener"><Field title /></a>
    </li>
  </Loop>
<Else />
  No other articles about <Format case=pascal><Route part=1 /></Format>.
</If>

Thank you and @Sunskie both, for your kind patience and assistance.

Thanks especially for that last example - I would never have figured it out otherwise.

Cheers!

2 Likes