Working on a publication (wordpress based) website, having a custom post type "article" with custom taxonomy : language (with only two options: French, English). I made a custom post type template (single-article-french.php) for only French language articles (English ones will be displayed using the single-cpt.php template). How can i make wordpress change automatically the template of my custom post type to single-article-french.php when the language of the article is set to French?

1 Answer 1


What you would do, is have your default single-article.php. This will get called by default because of the WP permalinks and templating system.

At the top of your single-article.php do the following before your get_header() call:

$language = get_the_terms( get_the_ID(), 'language' );

if ( ! is_wp_error( $language) && $language && 'French' == $language[0]->name ) {
    get_template_part( 'single-article-french' );
} else {
    // english template stuff
    // etc.

That way you will call single-article-french.php when the French category is selected.

  • thanks for your response. i got this : Fatal error: Cannot use object of type WP_Error as array in this line: if ( count( $language ) && 'French' == $language[0]->name ) { .
    – nisr
    Aug 5, 2015 at 14:43
  • 1
    We better add the is_wp_error( $language ) check as well, just in case. @nisr Most likely you got this error because it should be get_the_terms() instead of get_terms(). But then we must be careful here with count( $language ) for no terms, because count( false ) is 1 !! ;-)
    – birgire
    Aug 5, 2015 at 15:48
  • thanks @birgire. can you give your full suggested code please?
    – nisr
    Aug 5, 2015 at 16:02
  • 1
    it worked perfectly .. thanks a lot @birgire and thanks to Eric
    – nisr
    Aug 5, 2015 at 17:41
  • 1
    Thanks for the cleanup, @birgire, this answer was pre-coffee :). Aug 5, 2015 at 18:39

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.