1

I'm building a simple two language site. I'm using Polylang for posts, and also have a custom post type, "Artist." In order to keep it simple on the back-end, I'm not using Polylang, just a few custom fields for language on each artist.

When I load site.com/artist/johnny-cash I'm using the template single-artist.php on my theme. The question is, how would I manage to load a custom template when I load, for example, site.com/en/artist/johnny-cash?

So basically, this:

site.com/artist/johnny-cash -> Loads single-artist.php (awesome)

site.com/en/artist/johnny-cash -> How do I redirect to a custom template?

7
  • how have you enabled both URLs to point to your custom post type?
    – Milo
    Nov 10 '14 at 17:53
  • I'm not sure I understood you (probably didn't explained myself.) I registered a custom post type, and its slug is "artist", so WP automatically loads single-artist.php when you load /artist/john-doe
    – Nacho
    Nov 10 '14 at 17:56
  • and how does visiting that URL with /en/ prepended also result in your single post type being displayed? or does it presently 404?
    – Milo
    Nov 10 '14 at 17:59
  • 1
    WordPress tries to find what you meant to load before returning a 404, and redirects to that. so you have an additional step before you can deal with templates, and that's adding rewrite rules to make those /en/ requests load the correct object.
    – Milo
    Nov 10 '14 at 18:05
  • 1
    If you search add_rewrite_rule here you'll find lots of examples. I don't have time to write the code at the moment, but I can point you in a direction. When you add the rewrite rule to handle your custom post type requests with /en/, you can also add an extra query var, so you can identify those requests separately from the non en requests. then you can filter single_template, check for that query var, and load a special template in that case.
    – Milo
    Nov 10 '14 at 18:12
3

Thanks to @Milo on the comments, I'm gonna post my answer:

Firstly I added this rule:

add_rewrite_rule(  
    "en/artist/([^/]+)/?",  
    'index.php?pagename=post_type=artist&artist=$matches[1]&gal_template=en',
    "top"); 

Then this filter:

add_filter( 'query_vars', 'gal_query_vars' );
function gal_query_vars( $query_vars )
{
    $query_vars[] = 'gal_template';
    return $query_vars;
}

Finally, I filtered the single template:

add_filter( 'single_template', 'get_custom_post_type_template' );
function get_custom_post_type_template($single_template) {
     global $post;

     if ($post->post_type == 'artist') {

            $q = get_query_var('gal_template');

            if ($q === 'en') {
                $single_template = dirname( __FILE__ ) . '/single-artist-en.php';
            }


     }
     return $single_template;
}

Then on single-artist-en.php I did:

get_template_part('single-artist')

But you could do whatever you need.

1

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.