Zahari M. Posted March 7, 2014 Share Posted March 7, 2014 Hi guys! Hope someone can advise me with this! Very very new teritory for me this one... Ok... So I have a template that is used as a parent to display children... As there will be many children, results will be limited to 10 per page i.e. $posts = $page->children("limit=10,sort=-sort"); We will want to see the next set of 10 results of course so we call on our pager to advance to the next page... So $pagination = $posts->renderPager() sorts this out for us. So far so good. Ok.. so in my sites <head> document I want it to look like this.... <link rel="canonical" href="http://site.com/parent/page3/" /> <link rel="prev" href="http://site.com/parent/page2/" /> <link rel="next" href="http://site.com/parent/page4/" /> So I have found two helpful items in our beloved API: $page->httpUrl which will gives us the href="http://site.com/parent/ part needed. $input->pageNum which will tell us if we are on page2 or page3 etc. Lets say that we have a situation where there are 45 child page results. Well, we ourselves know that we will end up having 5 pages of results. So there will be parent parent/page2 parent/page3 parent/page4 parent/page5 My question is, when I am on parent/page5, which is the last page of results, how can I programatically determine that there is not a parent/page6? If I can determine this, then on parent/page5 i would be able to output this in my <head>. Note the absence of rel = 'next' <link rel="canonical" href="http://site.com/parent/page5/" /> <link rel="prev" href="http://site.com/parent/page4/" /> So if any of you have any ideas on how I would be able to test that I have come to the last page, I would be most grateful! Unfortunately, I have never ever worked with counters and modulus and so am somewhat at a loss as what is the best way to work this one out! Any help ideas etc most welcome! Link to comment Share on other sites More sharing options...
horst Posted March 7, 2014 Share Posted March 7, 2014 Hi Zahari, $yourRegularLimit = 10; $cur = $input->pageNum; $max = intval($pages->find("$yourSelector, limit=2")->getTotal() / $yourRegularLimit); // limit = 2 so it is fast and uses less memory! $next = $cur<$max ? $cur + 1 : false; $prev = $cur>1 ? $cur - 1 : false; echo '<link rel="canonical" href="http://site.com/parent/page' . $cur . '/" />'; if(false!==$prev) echo '<link rel="prev" href="http://site.com/parent/page' . $prev . '/" />'; if(false!==$next) echo '<link rel="next" href="http://site.com/parent/page' . $next . '/" />'; Not tested, just to ge you started, 5 Link to comment Share on other sites More sharing options...
Zahari M. Posted March 7, 2014 Author Share Posted March 7, 2014 Hi horst! Many thanks for this horst! I was looking at php's mathematical functions and the modulus operator and did come across getTotal() in the API. But couldn't workout how to arrange them to do the necessary last page check I needed. I see above that you use intval. I have never ever come across that before horst! It kinda seems to be exactly what I was looking to try and do with all the above mentioned items. Yay! I still don't fully understand how your code works just yet. But that's good! I'm going to go and have some lunch, find a good cafe somewhere where they serve both some good coffee and some good Shiraz. I shall start with the coffee, a good mechanical pencil, some paper and start to figure this one out step by step and expand my brains internal processing functions. Hopefully I can take my brain from a 386 to a 486 Then reward my self with some Shiraz! Or ask you for a little more assistance... Cheers horst! 2 Link to comment Share on other sites More sharing options...
teppo Posted March 7, 2014 Share Posted March 7, 2014 Quick tip: you can also use $pages->count($yourSelector) to get the number of matching pages. That's just a shorthand though, under the hood it does exactly what @horst did earlier 4 Link to comment Share on other sites More sharing options...
Zahari M. Posted March 7, 2014 Author Share Posted March 7, 2014 Ok horst and teppo... My head was spinning at first as I couldn't work out how to make my selector = child pages in the line below: $max = intval($pages->find("$yourSelector, limit=2")->getTotal() So what I did was: $currentPage = $page->id; then $max = intval($pages->find("parent=$currentPage, limit=2")->getTotal() / $maxPerPage); That got me my child pages and thus my $max value. The next problem I had was the next page link wasn't displaying a page number. This turned out to be here after some headscratching.... $next = $cur<$max ? $cur + 1 : false; When I changed the less than sign < to less than or equals <=, then that got it to work! Next thing was I didnt want to see "page1" on any of the urls. And so I set up a switch arrangment to provide me with the exact markup / structure that I wanted....a major reason why I love processWire !!! I have some ideas you see ... but not the skills like many of you great helpful guys here..... Anyway.... this is what I have come up with so far.... no coffee yet... but will have that right after posting this.... and drinks later tonight! // SEO - CANONICAL & REL SECTION: $maxPerPage = 10; $currentPage = $page->id; $curPageNum = $input->pageNum; $max = intval($pages->find("parent=$currentPage, limit=2")->getTotal() / $maxPerPage); // limit = 2 so it is fast and uses less memory! $next = $curPageNum <= $max ? $curPageNum + 1 : false; $prev = $curPageNum > 1 ? $curPageNum - 1 : false; switch ($input->pageNum){ case '1': echo "<link rel='canonical' href='$page->httpUrl'>\n"; if(false!==$next) echo "<link rel='next' href='$page->httpUrl" . 'page' . "$next" . "/'>\n"; break; case '2': echo "<link rel='canonical' href='$page->httpUrl" . 'page' . "$curPageNum/'>\n"; if(false!==$prev) echo "<link rel='prev' href='$page->httpUrl'>\n"; if(false!==$next) echo "<link rel='next' href='$page->httpUrl" . 'page' . "$next" . "/'>\n"; break; default: echo "<link rel='canonical' href='$page->httpUrl" . 'page' . "$curPageNum/'>\n"; if(false!==$prev) echo "<link rel='prev' href='$page->httpUrl" . 'page' . "$prev" . "/'>\n"; if(false!==$next) echo "<link rel='next' href='$page->httpUrl" . 'page' . "$next" . "/'>\n"; break; } So horst, thanks so much for helping out! Teppo thanks for coming in. If you guys were here I would buy you a nice cup of coffee! Cheers guys! Thanks a bunch! 3 Link to comment Share on other sites More sharing options...
horst Posted March 7, 2014 Share Posted March 7, 2014 Thanks for the coffee and the well formed and corrected code! Link to comment Share on other sites More sharing options...
larrybotha Posted April 16, 2014 Share Posted April 16, 2014 Just a note on SEO best practices - when correctly using rel next and prev, you can leave the canonical tag out on all paginated pages. This article has a great write up, but basically, next and prev make it redundant, and you can do serious damage to your SEO if implemented incorrectly with pagination. Better safe (and brief) than sorry! 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now