On this Craft Cms lesson you'll learn to output category based entries and the pagination variables.
It has been the best investment I made in my design journey so far. I would consider myself a novice but I honestly feel like I've gained a genuine professional insight into the process of creating a high-level website.
I'm an early adopter of the KreativePro Masterclass, and I love how well put together it is. Neftali takes you through each step of the process from paper sketches to marketing all with fun visuals & clear instructions!
Okay, welcome back guys to another lesson. If you remember, we created our movies and our actors page, but we haven't created one for our categories. So if we go into our movies categories and click on the settings. We can access the, we can give or assign a template for a specific category.
We want to keep things organized so let's create a new folder and we're going to do _categories. And inside that we're going to create a file for both movie genres and gain genres, in this case we're just going to focus on the movie template. We can, of course copy a lot of what we did for the movies. Copy that entire code and bring it over and paste it into our movie genres template because a lot of this has been repeated.
And so all we gotta do is change some of the variable information. So in this case, we want to relate this entry or this template to whatever the value that we're looking at. Let's say we're looking at action or crime, right? So this is what we want to do. Add in the parameter related to, to make that work.
Same goes with the count. And we got to make sure that we are pulling in the correct section. So we're calling in the movies in this case.
Next we can just remove the actual title and just bring in the title of the entry. In this case, the title would be crime or something like that.
We can have the movies button, take us back to the movies so we can keep that. Just need to use the correct routing. Once we refresh the page, we'll be able to see it under categories and movie genre. So once we refresh and click on any of these, for example, drama, we're going to see an issue happening.
And this is w this is the problem we're using entry as the actual object, but in this case, we're using categories. So all we have to do swap that out with categories. And also any time we use a object throughout the document, we got to make sure we use category instead of an entry. Once we do that, it'll work just fine.
So notice now the title works fine and we are only looking at movies related to the drama genera. All right. So let's clear this up. We don't need the count anymore. So I'm just going to remove this little snippet of code so we can introduce the pagination. So the way pagination works is by making sure that we have the function paginates at the very beginning, and we're going to call in the same thing.
So the block of craft entries and section, we still need that because we're pulling in that, the query of that information, and we're going to limit. The amount to only five entries. So what we want to do is pass in a couple of variables. The way we do that is by simply adding as page info, comma, page entries, these two variables take care of information about what the actual page you are on and the entries itself that were querying from the database.
So let's jump over to our for-loop. And change the variable to page entries because that's where we're fetching all that data. Just like that. We added that on the conditional and for the footer links, we're going to add a condition and ask the page info, variable. "Hey, if the previous URL exists, then I want you to output the correct link." So in this case, we pass in page info as the variable or the object. And we're got to to retrieve the previous URL. That's it. So we just got to a copy that paste it on our next URL. Change that to a next URL and add in the if condition.
Let's go ahead and refresh that and see how that works. Oops. Okay. I see. There's an issue happening. So on the very top of the document, we need to get rid of that set because we're not creating a variable called paginate. We're using a function called paginate. And just like that it works great. We have here our five entries that we are limiting and our next button that takes us to our page 2, notice how that's also added on our URL, we have our previous link as well, and it looks like it's working great.
One more thing we need to take care of is Google. Google likes to have links reference to this pagination system. So it's a best practice to have this. So what we're going to do is on our block of the head, we're going to add in the same concept we did with the footer, we're going to have a conditional calling page info prev URL, and we're going to output a link with some parameters. These parameters is the rail attributes and the href or the link. This is the URL. So we're just going to pull in the same variable page info dot prev URL. And we do the same thing, copy and paste for the next URL.
All right? So this is just best practice for Google to identify that this page does contain a pagination and it'll help a lot with your SEO.
Let's quickly inspect the documents and we'll see, once we toggle open the head, those links are right in there. Awesome. This has been an interesting module indeed. It's been a lot of information for you to swallow. So the next thing I'd like you to do is take what you've learned and applied. As a practice by installing craft on your system and trying to follow step-by-step what I'm doing with the work files.
If you think you're ready and you've practiced enough, let's jump into the next module and create our membership system. I'll see you soon.
Access our premium community including bonus tutorials, project files & weekly resources.LIFETIME 💎 ACCESS