-1

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

2 Answers2

0

Create a page template and use WP_Query to return the custom posts on that page. Then you can add in an AJAX filter to hide/show posts. I'm not going to write the entire code for you but here's the start from WP_QUERY documentation to show your posts.

// The Query returns all custom posts
$the_query = new WP_Query( 'post_type' => 'myalbums', 'posts_per_page'   => -1 );

// The Loop
if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
    /* Restore original Post Data */
    wp_reset_postdata();
} else {
    // no posts found
}
Andrew Schultz
  • 4,092
  • 2
  • 21
  • 44
  • My problem is not how do the query, is how create that custom URL and point it to `archive.php` file. :P – Sebas Fernandez Feb 27 '18 at 23:29
  • Use the filter template_redirect, https://wordpress.stackexchange.com/questions/173363/how-to-load-posts-to-a-custom-post-template-after-using-template-redirect-or-tem – Andrew Schultz Feb 28 '18 at 01:27
0

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'].