There was a need to detect if the page you are on is a “paginated” page, so I made a plugin. (And subsequently beefed it up with some suggestions in another thread, see below).
The forum thread that started all this: Detecting a single entry using EE code.
Working fine for me on a build right now. You can get the latest version here: MD Detect Page Type.
Sample Usage:
Super Basic Example:
{exp:md_detect_page_type url_segment="{segment_3}"}
{if pagination_page}This is a Paginated Page{/if}
{if category_page}This is a Category Page{/if}
{if yearly_archive_page}This is a Yearly Archive Page{/if}
{/exp:md_detect_page_type}
Basic Template:
{exp:md_detect_page_type url_segment="{segment_3}"}
{!-- if segment_3 is empty or it equals P## --}
{if pagination_page || segment_3 == ""}
{!-- list all the entries with pagination --}
{exp:weblog:entries weblog="weblog" paginate="bottom" limit="999"}
<h2>{absolute_count}. {title}</h2>
{custom_field}
{paginate}
<div class="pagination">
Page {current_page} of {total_pages} pages. {pagination_links}
</div>
{/paginate}
{/exp:weblog:entries}
{!-- otherwise, segment_3 has something in it
and it is not pagination. Show single entry --}
{if:else}
{exp:weblog:entries weblog="weblog" limit="1"}
{if no_results}Sorry! Couldn't find that entry.{/if}
<h2>{title}</h2>
{custom_field}
{/exp:weblog:entries}
{/if}
{/exp:md_detect_page_type}
If you use this and find any weird cases that I may have missed, let me know.
Add: A thread called “Can’t get pagination to work” deals with this pagination issue using PHP.
I am having a problem with this.
If the 3rd segment contains a non-existent url title it just shows the last item and ignores the {if no_results} conditional.
If you add require_entry=”yes” to the second weblog:entries tag it displays just the Sorry message with no other HTML. This fine if there is no other content, but I have extra content after this and it sends that HTML to the browser but without any of the previous code(like the embeded header template).
But if you use {redirect="404"} instead of the Sorry message it redirects to the 404 if segment_3 is empty or contains a non-existent url title.
I would rather re-direct to a 404, so is there a tweak to the plug-in to fix this?
Below is my code, the same as yours except you were missing the closing {/pagination} tag and had an extra {/if} in the first condition.
{exp:md_detect_page_type url_segment="{segment_3}"}
{!-- if segment_3 is empty or it equals P## --}
{if pagination_page || segment_3 == ""}
{!-- list all the entries with pagination --}
{exp:weblog:entries weblog="support" paginate="bottom" limit="2"}
<h2><a href="http://{comment_url_title_auto_path}">{title}</a></h2>
{summary}
{paginate}
<div class="pagination">
Page {current_page} of {total_pages} pages. {pagination_links}
</div>
{/paginate}
{/exp:weblog:entries}
{!-- otherwise, segment_3 has something in it
and it is not pagination. Show single entry --}
{if:else}
{exp:weblog:entries weblog="support" limit="1"}
{if no_results}Sorry! Couldn't find that entry.{/if}
<h2>{title}</h2>
{body}
{/exp:weblog:entries}
{/if}
{/exp:md_detect_page_type}
I fixed my code example above; thanks for pointing that out.
If the 3rd segment contains a non-existent url title it just shows the last item and ignores the {if no_results} conditional. If you add require_entry=”yes” to the second weblog:entries tag it displays just the Sorry message with no other HTML. This fine if there is no other content, but I have extra content after this and it sends that HTML to the browser but without any of the previous code(like the embeded header template).
That’s how it’s supposed to work, correct? “require_entry” and “if no_results” work in tandem. If there are no results, then you’re supposed to get back what is in the no_results conditional.
But if you use {redirect="404"} instead of the Sorry message it redirects to the 404 if segment_3 is empty or contains a non-existent url title. I would rather re-direct to a 404, so is there a tweak to the plug-in to fix this?
Hmm, after doing some more testing, I see what you’re saying. Let me look into this.
OK, I just did a bunch of testing and I don’t know why having the “redirect=’404’” in there messes it up, but I did fine that by embedding just one more level, you can get around the problem. I put this on my template, and have it working with a 404 redirect on the single entry template:
{exp:md_detect_page_type url_segment="{segment_3}"}
{if segment_3 == ""}
{embed="group/_entries_list"}
{/if}
{if segment_3 != ""}
{if pagination_page}
{embed="group/_entries_list"}
{if:else}
{embed="group/_single_entry"}
{/if}
{/if}
{/exp:md_detect_page_type}
This also works when putting only the single entry result with the 404 redirect on its own template and embedding, but I just decided to embed both the list and the single entry to keep things clean on the main template.
Maybe you could try putting this:
{exp:weblog:entries weblog="support" limit="1"}
{if no_results}Sorry! Couldn't find that entry.{/if}
<h2>{title}</h2>
{body}
{/exp:weblog:entries}
in an embed to see if that takes care of the problem?
Sorry Ryan, I didn’t get an email that you had replied.
I don’t really want to have lots of embeds in the page so would prefer to keep it all in the one template.
Have you had any luck finding out where the problem is with the:
{if no_results}Sorry! Couldn't find that entry.{/if}
and it being ignored and just showing the last entry in the weblog.
I do use embeds for header, sidebar and footer, but I don’t want to confuse the client with loads of other bits that aren’t intuitive.
I develop some sites for someone else to take over after I have done all the configuration and EE’d the templates. They have HTML skills and plan to learn the EE stuff as they go along, with me providing support if they get stuck.
That’s why this extension looked attractive instead of a load of PHP and RegEx in the template.
Hey Ryan,
I am using this plugin quite a bit on a site that I am developing, it’s been really handy. I just had a quick question about how the plugin works. Say I have 3 conditions, the basic landing/pagination page, a category view and a yearly archive view. Does ExpressionEngine actually parse all 3 conditions and then display only the one that MD Detect Page type instructs it to? OR does it run thru the plugin and only parse whichever condition is met by the URL_Segment?
Just curious.
The plugin should do it’s work after EE does its thing. If you have a category page, EE knows and will try to do the automatic category things it is built to do - same with yearly archives, etc. The plugin is just a way, after the fact, to help control what is shown.
This was originally very simple and was used to help me show a single entry on an “index” template that might have Pagination. In either case something will be in segment_3. I just wanted to make sure that if segment 3 starts with a capital “P” and is followed by a number to show paginated entries like it should (and not a 404 page because I don’t have an entry named “P20” or “P40”), and if not, show a single entry.
Ahh I see, well I am actually using it right now on a site I am working on. I have it setup to output 4 different cases of views (paginated, category, archive, single) and I believe it is outputting all 4 groups of entry tags every time, from what I can tell from the template debugging output. This is a pretty handy plugin for sure, thanks!
Actually this isn’t work for me after pasting all the examples from the site, where I’m sure that my segment_3 is a category. Is there anything to about in the control panel to start detecting the categories OR maybe this detects only categories, which have format Cxx or Category/XX as my segments are actually the category url’s
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.