2

I have a URL like http://example.com/arbitrary/path that I would like to map to a specific template file. At the moment I am achieving this by enabling permalinks and creating the pages "arbitrary" and "path" (page "arbitrary" is set to parent of "path") then choosing a custom template. The downside to this is that the client could delete these pages thus breaking the site.

Anyone know of a better way? Like something I could add to functions.php.

1
  • Yeah, and if there is a contact page and the client delete it, the site will break too. I think that the reason you are exposing is worthless. Can you deeply explain the context? – cybmeta Jan 3 '16 at 14:01
2

Please place below code in your theme's functions.php file & let me know how it goes :)

add_action('wp', function(){
    list($uri, $qs) = explode('?', $_SERVER['REQUEST_URI']);
    if ( $uri == "/arbitrary/path" ) /* Change this to path that you want to match*/
    {
        locate_template( "template-full-width.php" , true, false ); /* Don't forget to replace template name with actual template which you want to load */
        die(); /* So that WordPress does not load its template as per template hierarchy. */
    }         
});
1
  • Thanks mate works perfect! You even took into account I may be using a query string. – Tom Berghuis Jan 3 '16 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.