Displaying Entries and Pagination in our ExpressionEngine Website
I’m embarassed to say that its four days shy of four months since the last article in this series. Between travel, kids, client work and the holidays, who has time to write? Well, I am back and have re-read all of the previous installments to this series on BabyFilter.com. I am happy to say that, other than a few minor changes to my workflow, everything you’ve read still is current and completley applicable to the project.
Way back in September, I wrote in the last article about listing out our Topic categories in the left sidebar of the site. To refresh, take a quick look at the staging site.
Today, we’ll focus on getting Topics onto the homepage, the main Topics page, and the individual Topic pages. I spent a couple of hours creating member accounts for all the members of the old site and entering their old topics. This gives us some nice data to work with as we set up the site. I haven’t brought any comments over yet, so all of our topics will reflect 0 comments for now.
Showing Entries on the Homepage
Let’s quickly take stock of the current homepage code:
{embed="includes/header_top" title="Homepage"}
<!-- Place any additional JS and CSS links or code here -->
{embed="includes/header_bottom" loc="home"}
<p>Content</p>
{embed="includes/footer"}
Pretty simple. We’ll want any content on the page to fall between the header_bottom embed and the footer embed. On the current site, you’ll see we have a boxed in introduction paragraph, followed by a date-based listing of topics, with the most recent one at the top. We’ll skip the intro paragraph for now and concentrate on the topics.
The first thing I will do is replace the placeholder Content paragraph with a typical ExpressionEngine weblog entries tag.
{embed="includes/header_top" title="Homepage"}
<!-- Place any additional JS and CSS links or code here -->
{embed="includes/header_bottom" loc="home"}
{exp:weblog:entries weblog="topics" disable="categories|category_fields|member_data|pagination|trackbacks" rdf="off"}
<div class="topic-list-box">
{title}
</div>
{/exp:weblog:entries}
{embed="includes/footer"}
Some things to note: I have specified the weblog and disabled quite a few types of data in order to speed up the rendering of this list of Topics. One thing I didn’t include in the disable parameter is custom_fields. We’ll be relying on quite a few custom fields in the Topics section.
I’ve created a div called topic-list-box to wrap each topic in so that we can style each one as necessary. I won’t show you the full code from the old site; since it is PHP its pretty long. But, here is the format we want to get to for each topic-list-box div:
<p class="datehead">May 14th</p>
<p class="postsum">Does anyone know of a travel website that has recommendations of places to rent for vacation that are kid-friendly? We are currently looking for a place in Lake Tahoe, CA and I cant find anything that specializes in "kid-friendly" (ideally somewhat childproofed) rentals (homes or condos). I am also looking for something in Kauai. Does anyone know of a website that can help?</p>
<p class="postbyline">posted by smj1 to <a href="/category.php?id=25">Travel</a> at 5:10 PM PDT - <a href="/post/?id=91">0 responses</a></p>
I’ll likely clean up those class names later to make them more appropriate to the data we are showing, but for now let’s just keep them. I’ll skip the p class=“datehead” date header right now and focus on the topic data.
So, by combining our new ExpressionEngine template with the content from the old site, here is the framework for the div with the static data:
{embed="includes/header_top" title="Homepage"}
<!-- Place any additional JS and CSS links or code here -->
{embed="includes/header_bottom" loc="home"}
{exp:weblog:entries weblog="topics" disable="categories|category_fields|member_data|pagination|trackbacks" rdf="off"}
<div class="topic-list-box">
<p class="postsum">Does anyone know of a travel website that has recommendations of places to rent for vacation that are kid-friendly? We are currently looking for a place in Lake Tahoe, CA and I cant find anything that specializes in "kid-friendly" (ideally somewhat childproofed) rentals (homes or condos). I am also looking for something in Kauai. Does anyone know of a website that can help?</p>
<p class="postbyline">posted by smj1 to <a href="/category.php?id=25">Travel</a> at 5:10 PM PDT - <a href="/post/?id=91">0 responses</a></p>
</div>
{/exp:weblog:entries}
{embed="includes/footer"}
Now, to take this one home, let’s replace the static data with the correct fields that we created earlier:
{embed="includes/header_top" title="Homepage"}
<!-- Place any additional JS and CSS links or code here -->
{embed="includes/header_bottom" loc="home"}
{exp:weblog:entries weblog="topics" disable="categories|category_fields|member_data|pagination|trackbacks" rdf="off"}
<div class="topic-list-box">
<p class="postsum">{title}</p>
<p class="postbyline">posted by {author} to <a href="/category.php?id=25">Travel</a> on {entry_date format="%D, %F %j%S"} at {entry_date format="%g:%i"} - <a href="/post/?id=91">{comment_total} responses</a></p>
</div>
{/exp:weblog:entries}
{embed="includes/footer"}
I didn’t replace every single variable yet, but I did get the basics in there. You can see here that we are already well on our way:

Categories
I’ve got the title, author’s screen name, date, time, and total comments for each topic in place. Let’s fix up the categories- right now they all show Travel, as that is the category that I copied along with the rest of my sample topic from the old site.
The first thing we need to do is enable categories in our entries tag by removing “categories” from the disable parameter. The new entries tag is:
{exp:weblog:entries weblog="topics" disable="category_fields|member_data|pagination|trackbacks" rdf="off"}
Now, to display each topics’ category, we need to wrap the part of the entry that displays the category info in ExpressionEngine “categories” tags. So
<a href="/category.php?id=25">Travel</a>
becomes
{categories}<a href="/category.php?id=25">Travel</a>{/categories}
Next, I’ll replace the sample category name:
{categories}<a href="/category.php?id=25">{category_name}</a>{/categories}
And finally the link:
{categories}<a href="{path=topics}">{category_name}</a>{/categories}
Now, with a refresh, we have the correct category for each topic, along with a link to each specific categary page of Topics.

Limiting the Number of Entries
The next thing that needs attention, though it is unique to this situation, is the entries being shown. Typically, ExpressionEngine will show all entries with a status of Open and an entry date prior to right now. When I recreated these topics in the new system, I forward dated them exactly two years. That means that some of them have an entry date of late 2008/early 2009, and some have an entry date after today. The system is not showing any entries in our list that have an entry date after today. In order to show these, I will add show_future_entries=“yes” to the weblog entries tag.
{exp:weblog:entries weblog="topics" disable="category_fields|member_data|pagination|trackbacks" rdf="off" show_future_entries="yes"}
That’s better. You may or may not want this functionality based on the needs of your application.

Linking to the Author’s Profile
Finally, we’ll want to add some if/then code to allow a link to the author’s profile. If the viewing user is logged in, they’ll be able to click the author’s screen name to find out more about them. If the user is logged out, no link. Here is the code we’ll be working with:
posted by {author}
We’ll need to change it to this:
posted by {if logged_in}<a href="{path=member}{author_id}">{author}</a>{/if}{if logged_out}{author}{/if}
This simply shows a link to the author’s profile if the user is logged in. Note the link syntax; in order to get a slash and then the author_id correct in the URL, separate the path call and the author_id call into separate ExpressionEngine tags.
Linking to the Single Entry Page
Now we just need to update the link to the individual topic itself. We’ll use the URL Title so that we get readable URLs, so we need to replace this
<a href="/post/?id=91">
with this
<a href="{url_title_path=topics/view">
Very simply, if we have a topic with the URL Title “my_topic”, the link created by ExpressionEngine will be /topics/view/my_topic/.
The Date Header
The only thing left to do is display the date header. I’ll tackle that, for the most part, in a later article. For now, we’ll just add each topic’s date above it so the layout element is in place. Above the topic data, I’ll place this code:
<p class="datehead">{entry_date format="%F %j%S"}</p>

For the homepage I only want to show, say, the 10 most recent topics. This is easily accomplished by putting a limit into the weblog entries tag:
{exp:weblog:entries weblog="topics" disable="category_fields|member_data|pagination|trackbacks" rdf="off" show_future_entries="yes" limit="10"}
I’ll throw a link at the bottom of the list, after the closing weblog entries tag, that allows users to read more topics (on the main Topics page) so there is a call to action at the bottom of the page.
So, here is our entire code for the homepage, which is, for now, done:
{embed="includes/header_top" title="Homepage"}
<!-- Place any additional JS and CSS links or code here -->
{embed="includes/header_bottom" loc="home"}
{exp:weblog:entries weblog="topics" disable="category_fields|member_data|pagination|trackbacks" rdf="off" show_future_entries="yes" limit="10"}
<div class="topic-list-box">
<p class="datehead">{entry_date format="%F %j%S"}</p>
<p class="postsum">{title}</p>
<p class="postbyline">posted by {if logged_in}<a href="{path=member}{author_id}">{author}</a>{/if}{if logged_out}{author}{/if} to {categories}<a href="{path=topics}">{category_name}</a>{/categories} on {entry_date format="%D, %F %j%S"} at {entry_date format="%g:%i"} - <a href="{url_title_path=topics/view}">{comment_total} responses</a></p>
</div>
{/exp:weblog:entries}
<p><a href="{path=topics}">Read more topics ></a></p>
{embed="includes/footer"}
It’s really quite simple.
The Main Topics Page
Now, since it is so easy, I’ll use this same code to show entries on the main Topics page. Right now, the /topics/index template blank. I’ll first copy the entire homepage template and paste it into the /topics/index template.
To make the main Topics page do what we want, we just need a few adjustments.
- Change the “title” variable in the header_top embed to “Topics”
- Change the “loc” variable in the header_bottom embed to “topi”
- Change the “limit” parameter in the weblog entries tag to 15
- Remove “pagination|” from the disable parameter of the weblog entries tag
- Add paginate=“bottom” to the weblog entries tag
- Remove the “read more topics” paragraph tag and link from the bottom of the page
Save it and upload, and click over to the main topics page and you should see a page identical to the homepage, but with a different title, more topics, and if we had a style for the “you are here” main menu item, an indicator of that. Really, this is all we need on this page, except for pagination.
Pagination
ExpressionEngine pagination is pretty easy. I have a pre-written block of code I use for it:
{paginate}
{if "{total_pages}" != 1}<p class="pagination">Page {current_page} of {total_pages} pages {pagination_links}</p>{/if}
{/paginate}
This, along with the changes we made to the weblog entries tag above, places a nicely functioning set of pagination links at the bottom of the page. Since we’ve wrapped it all in a p tag with a class of “pagination”, we can easily style it later. The links will update automatically based on how many pages worth of topics are in the system. You can use the “limit” parameter to show more or less topics on a page and change the total number of pages for the section, if necessary.

Well, I think that is quite enough for this resurrection. Here’s to hoping I can continue this next week!

4 Comments
posted on August 30, 2009 at 7:45am by Yikes:
Excellent tutorials, I read every word from the beginning to the end. Thanks!
I’m still a beginner, but one thing about EE that bugs me is that it’s not very straight forward to create a structure such as:
domain.com/index.php/news/international/article_title_1
where “news” is a weblog, “international” is a category and “article_title_1” is a weblog entry in the “international” category.
That was one of the first problems I had as a beginner and even though I think I may have managed to get it working, I’m not sure if I’m doing it the right way. Unfortunately, it hasn’t been covered in your tutorials, but I learned much from it either way, thanks again!
posted on August 30, 2009 at 8:06pm by Chad Crowell:
Hi Yikes -
Your desired URL structure won’t natively work in EE, though you could get it to work with some advanced setup.
Natively, the URL structure is domain.com/index.php/news/article/article_title_1
The biggest lesson here is that while news is the name of my template group and article is the name of my template, that news does not have to be the name of my weblog. The weblog I am pulling data from for this page could be called “donuts” or anything I want. So don’t feel trapped into a particular URL structure based on your weblog name. Now, naming the weblog “news” and the template group “news”, in this case, works great.
One thing you are doing wrong is placing the category url_title into this URL. You *can* do this with EE, but the above paragraph describes the default structure. A more likely scenario for you would be:
- domain.com - home
- domain.com/index.php/news - the main news section page
- domain.com/index.php/news/category/international - the main news section page filtered to show just entries in the international category
- domain.com/index.php/news/article/article_title_1 - the article detail page
In order to do what you want, on the international category page, you’d need to create your links to the individual articles like so:
<a href="{path=news/international}{url_title}">{title}</a>You can hard code news/international because you are already on the international category page, so you know that all links to entries from this page will use news/international.
The trick here is that you are calling the news/index template to show this article, and that is the same page you are already on! So you will need something like:
{if segment_3 != '' AND segment_2 != 'category'}(show your single entry article code here}
{if:else}
(show the main news page listing code)
{/if}
Since the main news page and the category news pages use the same code (or almost the same), the logic here is that you are showing the main news page if segment 3 of the URL is empty or if segment 2 does not equal ‘category’ (which is the URL trigger to show a category page).
Finally, to pull this off, on the single entry weblog call, you need to explicitly tell EE to use segment 3 as the url_title trigger for the entry and ignore everything else in the URL:
{exp:weblog:entries weblog="news" url_title="{segment_3}" dynamic="off"}That should get you there. Might be confusing at first but once you get it working you’ll feel the lightbulb come on. Good luck.
posted on August 31, 2009 at 2:40pm by Yikes:
Thanks for the detailed reply Chad! You are right by mentioning the lighbulb moment..I just know I’m getting very close
Your explanation makes perfect sense and it’s very similar to what I had already done. It really helps to see a good example with comments from a more experienced EE user.
The biggest reason for my initial confusion is the category trigger. In my previous CMS, the whole category concept is different. When I add content, I add it to a category and to show all items in the category, the URL would be for example: domain.com/news/international
I’ve only build one site with EE so far, so it’s still something I have to get used to, but I’m learning every day. Almost time to get started on the second EE site!
Thanks again! Your tutorials have been very helpful.
posted on November 22, 2009 at 3:16am by Dave:
Chad: thanks for the articles. I’m anxiously awaiting your next one. These have been great.
I’ve been looking for just this sort of tutorial for EE, glad I found yours. I don’t know if you use “related fields” but that would be very helpful to cover if you do.
Thanks again, keep up the good work!
Leave a Comment