Plugin: MD Detect Page Type. Pagination, categories, yearly archives and single entries all on one template.
Posted: 28 September 2008 10:06 PM   [ Ignore ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  2021
Joined  03-26-2006

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}<br />
{if category_page}This is a Category Page{/if}<br />
{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>
    <
p>{custom_field}</p>
    
{paginate}
    
<div class="pagination">
    <
p>Page {current_page} of {total_pages} pages. {pagination_links}</p>
    </
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}
<p>Sorry! Couldn't find that entry.</p>{/if}
    <h2>{title}</h2>
    <p>{custom_field}</p>
    {/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.

 Signature 

ryan masuga
—————
Masuga Design (EE Pro) | devot:ee
MD Markitup | All Add-Ons
Twitter: masuga | masugadesign | devot_ee

Profile
 
 
Posted: 01 October 2008 08:55 AM   [ Ignore ]   [ # 1 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  209
Joined  11-22-2006

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="{comment_url_title_auto_path}">{title}</a></h2>
    <
p>{summary}</p>
    
{paginate}
    
<div class="pagination">
    <
p>Page {current_page} of {total_pages} pages. {pagination_links}</p>
    </
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}
<p>Sorry! Couldn't find that entry.</p>{/if}
    <h2>{title}</h2>
    <p>{body}</p>
    {/exp:weblog:entries}

  {/if}
{/exp:md_detect_page_type}
 Signature 

pfweb.co.uk

Profile
 
 
Posted: 01 October 2008 09:58 AM   [ Ignore ]   [ # 2 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  2021
Joined  03-26-2006

I fixed my code example above; thanks for pointing that out.

orienteer - 01 October 2008 08:55 AM

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.

orienteer - 01 October 2008 08:55 AM

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.

 Signature 

ryan masuga
—————
Masuga Design (EE Pro) | devot:ee
MD Markitup | All Add-Ons
Twitter: masuga | masugadesign | devot_ee

Profile
 
 
Posted: 01 October 2008 01:39 PM   [ Ignore ]   [ # 3 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  2021
Joined  03-26-2006

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}
<p>Sorry! Couldn't find that entry.</p>{/if}
    <h2>{title}</h2>
    <p>{body}</p>
    {/exp:weblog:entries}

in an embed to see if that takes care of the problem?

 Signature 

ryan masuga
—————
Masuga Design (EE Pro) | devot:ee
MD Markitup | All Add-Ons
Twitter: masuga | masugadesign | devot_ee

Profile
 
 
Posted: 23 October 2008 03:30 PM   [ Ignore ]   [ # 4 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  209
Joined  11-22-2006

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}<p>Sorry! Couldn't find that entry.</p>{/if}

and it being ignored and just showing the last entry in the weblog.

 Signature 

pfweb.co.uk

Profile
 
 
Posted: 23 October 2008 03:45 PM   [ Ignore ]   [ # 5 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  2021
Joined  03-26-2006

Not sure. I know the embed takes care of it. Probably a parse order issue. I’m not exactly sure how parse=“inward” works but maybe that could be used if you don’t want to use an embed?

With all due respect, how do you get by without embeds!?

 Signature 

ryan masuga
—————
Masuga Design (EE Pro) | devot:ee
MD Markitup | All Add-Ons
Twitter: masuga | masugadesign | devot_ee

Profile
 
 
Posted: 23 October 2008 04:25 PM   [ Ignore ]   [ # 6 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  209
Joined  11-22-2006

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.

 Signature 

pfweb.co.uk

Profile
 
 
Posted: 10 March 2009 12:34 AM   [ Ignore ]   [ # 7 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  527
Joined  12-15-2005

is it possible to check if the page is NOT a pagination page? like:

if !pagination_page

?

 Signature 

System Info: Production

EE Version:  1.67
Build
:       20090320
PHP Version
: 5.2.5
Profile
 
 
Posted: 10 March 2009 12:40 AM   [ Ignore ]   [ # 8 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  2021
Joined  03-26-2006

Could you just do a block like this?

{if pagination_page}
{if
:else}
...do or embed something...
{/if}
 Signature 

ryan masuga
—————
Masuga Design (EE Pro) | devot:ee
MD Markitup | All Add-Ons
Twitter: masuga | masugadesign | devot_ee

Profile
 
 
Posted: 14 March 2009 09:18 AM   [ Ignore ]   [ # 9 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  527
Joined  12-15-2005

Hi,
I actually needed to do something specifically only if it wasn’t a pagination page. I ended up with this, which works nicely:

{exp:md_detect_page_type url_segment="{segment_9}"}
   {if segment_9
!= "" AND pagination_page != TRUE}
      
do stuff here...
   
{/if}
{
/exp:md_detect_page_type}
 Signature 

System Info: Production

EE Version:  1.67
Build
:       20090320
PHP Version
: 5.2.5
Profile
 
 
Posted: 14 March 2009 10:38 AM   [ Ignore ]   [ # 10 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  2021
Joined  03-26-2006

Cool, thanks for sharing. I’ll add that bit of code to the page for this add-on over at Masuga Design.

 Signature 

ryan masuga
—————
Masuga Design (EE Pro) | devot:ee
MD Markitup | All Add-Ons
Twitter: masuga | masugadesign | devot_ee

Profile
 
 
Posted: 25 May 2009 09:31 AM   [ Ignore ]   [ # 11 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  265
Joined  03-01-2006

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.

Profile
 
 
Posted: 26 May 2009 09:04 PM   [ Ignore ]   [ # 12 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  2021
Joined  03-26-2006

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.

 Signature 

ryan masuga
—————
Masuga Design (EE Pro) | devot:ee
MD Markitup | All Add-Ons
Twitter: masuga | masugadesign | devot_ee

Profile
 
 
Posted: 26 May 2009 09:12 PM   [ Ignore ]   [ # 13 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  265
Joined  03-01-2006

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!

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 77557 Total Logged-in Users: 52
Total Topics: 101552 Total Anonymous Users: 27
Total Replies: 544387 Total Guests: 287
Total Posts: 645939    
Members ( View Memberlist )