4

some templates are chosen from WordPress based on slugs (e.g. category-{slug}.php).

Problem: How can I use the same template for an i18n'ed slug?

Example: I have a category-news.php and an english category with slug "news" and german category with slug "nachrichten". But of course "nachrichten" doesn't get category-news.php as a template. How can I i18n "nachrichten" so WordPress knows it means "news" before the template will be chosen internally?

1 Answer 1

5

Filter template_include:

add_filter( 'template_include', 'prefix_translate_template' );

function prefix_translate_template( $template )
{
    if ( 'category-' . __( 'news', 'your_textdomain' ) . '.php' === $template )
        return 'category-news.php';

    return $template;
}

But I think templates based on slugs are not a good idea in that case.

4
  • I didn't know the template_include filter. That seems to exactly solve my problem. Thank you very much. Any reasons why you wouldn't use slugs in this case? They seem to be very handy.
    – Pipo
    Apr 4, 2013 at 9:20
  • You have to create a new rule for each category now. That's not elegant and probably slow and error prone. I wouldn't do that. A term meta field with a template selector in the back-end is probably better.
    – fuxia
    Apr 4, 2013 at 9:23
  • You are right, especially because we use a lot of categories and a lot of languages (not just english and german). I searched for something like a "template-chooser" (like implemented in pages) before, but couldn't found something useful. I thought WordPress doesn't support something like this outside of pages?
    – Pipo
    Apr 4, 2013 at 9:26
  • 1
    There are workarounds for the missing term meta table on our site. :) You could also just use one category template and handle all the conditions in that file.
    – fuxia
    Apr 4, 2013 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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