Jump to content

Cannot get() a page by path if it ends with "pageN"


Valery
 Share

Recommended Posts

Hello everyone,

I use an external page ranking service which gives me back URLs of the most popular pages from my PW-powered web site.

To display page titles instead of URLs to my visitors, I try to call $pages->get(...)->title to get a title for each page.

Generally it works, but there is a problem: if a page uses a pagination-enabled template, I often end up with URLs like http://host/gallery/page10. And $pages->get("/gallery/page10") gives me a '0'.

So, is there a common approach in ProcessWire to find a page by its URL if its template uses pagination?

Thanks.

Link to comment
Share on other sites

A well-formed question is half the answer ;)

Actually, the answer can be found here: http://stackoverflow.com/a/4349042

Which can be roughly implemented as:

$url = 'http://host/gallery/page3';
    
if ($pages->get(parse_url($url, PHP_URL_PATH))->id == 0) {
    $doc = new DOMDocument();
    @$doc->loadHTMLFile($i->url);
    $xpath = new DOMXPath($doc);
    $title = $xpath->query('//title')->item(0)->nodeValue . "\n";
} else {

    $title = $pages->get(parse_url($url, PHP_URL_PATH))->title;
} 

Summary: if you cannot get a page by its path, then you will have to do it the hard way.

Link to comment
Share on other sites

$pages->get("/gallery/page10") gives me a '0'.

That's because the "page10" portion is not part of the page's path in the system. It's just an extra instruction to the page, like a URL segment, telling it to use the 10th pagination. In this case, you'd want to trim off that "page[n]" portion. There's any number of ways to do that, but I'd probably just use a regex: 

$title = 'http://host/gallery/page3';
$title = preg_replace('{page[\d+]/?$}', '', $title); 
echo $title; // should output "http://host/gallery/"
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...