Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like to display automatically a post attachment (PDF) depending on the language of my blog (french and english).

When the post is displaying in french version, I would like display the french PDF, and when the post is displaying in english, the english one.

I use the qtranslate plugin and I though to use the caption of the attachment ("fr" or "en") to create a sort of conditional tag.

I tried the code below but it does'nt work. Do you have any idea to help me?

Many thanks in advance, Dem.

<!-- PDF EN -->
<?php if(qtrans_getLanguage()=='en'): ?>
    <?php
        if ( $attachments = get_children( array(
            'post_type' => 'attachment',
            'post_mime_type' => array('application/doc','application/pdf','application/msword'),
            'numberposts' => 1,
            'post_status' => null,
            'post_parent' => $post->ID,
            ))) ;
            foreach ($attachments as $attachment) {
            if ($attachment->post_excerpt == 'en') {
                echo '<a href="' . wp_get_attachment_url( $attachment->ID ) . '"><img src="' .get_bloginfo('template_directory') . '/images/pdf.png" alt="Pdf" class="pdf" /></a>';
                echo '';
            }
            }
    ?>
<?php endif; ?>
<!-- PDF FR -->
<?php if(qtrans_getLanguage()=='fr'): ?>
    <?php
        if ( $attachments = get_children( array(
            'post_type' => 'attachment',
            'post_mime_type' => array('application/doc','application/pdf','application/msword'),
            'numberposts' => 1,
            'post_status' => null,
            'post_parent' => $post->ID,
            ))) ;
            foreach ($attachments as $attachment) {
            if ($attachment->post_excerpt == 'fr') {
                echo '<a href="' . wp_get_attachment_url( $attachment->ID ) . '"><img src="' .get_bloginfo('template_directory') . '/images/pdf.png" alt="Pdf" class="pdf" /></a>';
                echo '';
            }
            }
    ?>
<?php endif; ?>
share|improve this question

1 Answer 1

Can you try something along these lines and report back the result?

<?php if(qtrans_getLanguage()=='en'): ?>

  // attachment if statement


<?php else if(qtrans_getLanguage()=='fr'): ?>

  // attachment if statement

<?php endif; ?>

Technically you could omit the second else if statement and replace it just with <?php else : ?> assuming you only have two languages you are dealing with because if the first condition fails then it will automatically run your second if statement for your attachment.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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