Search code examples
wordpresstimber

Wordpress alternative URL to archive page


I have a custom post type named myalbums, so the url to archive page is http://mysite/gallery (I added 'rewrite' => ['slug' => 'gallery'] on it's creation).

So, I want to have an alternative URL like http://mysite/gallery/special to see the same page but with a little filter. On one page you can see some albums and on the alternative you can see anothers.

How can I do this, and how can I know when user is on one of them?

PS. I use Timber Wordpress


Solution

  • I just used rewrite api like this:

    add_filter('query_vars', 'add_query_vars');
    function add_query_vars($aVars) {
        $aVars[] = "my-var";
        return $aVars;
    }
    
    add_filter('rewrite_rules_array', 'add_rewrite_rules');
    function add_rewrite_rules($aRules) {
        $aNewRules = array('gallery/special/([^/]+)/?$' => 'index.php?post_type=myalbums&my-var=$matches[1]');
        $aRules    = $aNewRules + $aRules;
        return $aRules;
    }
    

    No accessing to http://mysite/gallery/special/anything this request goes to archive.php file and I can get the var just calling $wp_query->query_vars['my-var'].