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:
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,
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
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.
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!
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.
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”?
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.
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
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:
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:)