0

I am trying to make a loop where I display all my taxonomies with the first post of every taxonomy. It's the taxonomy "customers" and I want to display every first portfolio-CPT of the customer.

I have polylang with english and german language installed, all working, instead of this loop.

The Loop is working, but the project always get me the english posts. No matter what language it's set.

Here is a pastebin, just to be clear the customer tax is "kunden" and the custom post type is "portfolio". The "kunden"-tax is not translated, both language use the same one. The "portfolio"-cpt is translated.

<?php get_header() ?>

<div class="row" id="main">
        <div class="large-12 columns">
                <ul class="small-block-grid-3 large-block-grid-6">
                <?php

                $lang = pll_current_language();

                $terms = get_terms( 'kunden' );

                foreach( $terms as $term ) :

                        $args = array(
                                'post_type' => 'portfolio',
                                'kunden' => $term->slug,
                                'posts_per_page' => 1,
                                'lang' => $lang,
                        );
                        $single = new WP_Query( $args ); ?>
                        <?php if ($single->have_posts()) { ?>

                        <?php while ($single->have_posts()) : $single->the_post(); ?>

                        <?php get_template_part('project-thumb'); ?>

                        <?php endwhile; ?>

                        <?php } ?>
                <?php endforeach; ?>
                </ul>
        </div>


</div>


<?php get_footer() ?>

If I change post_per_page => 2 the german post is the second one after the english one. So both Languages are displayed one after another.

3

1 Answer 1

1

i finally figured out a solution. I guess it's not the perfect one, because all posts are still looped and the wrong language just doesn't show, but for now it works. I am still open for ideas.

My idea right now is to get the language of the post and don't display it if it doesn't match the pages language.

Here's the new part.

global $polylang;
$postlang = $polylang->get_post_language(get_the_ID())->slug;

if ($postlang == $lang) {
    get_template_part('project-thumb');
}

The full code now looks like this and is working for me.

<?php get_header() ?>

<div class="row" id="main">
    <div class="large-12 columns">
        <ul class="small-block-grid-3 large-block-grid-6">
        <?php
        global $polylang;
        $lang = pll_current_language();

        $terms = get_terms( 'kunden' );

        foreach( $terms as $term ) :

            $args = array(
                'post_type' => 'portfolio',
                'kunden' => $term->slug,
                'posts_per_page' => 2,
                'lang' => $lang,
            );
            $single = new WP_Query( $args ); ?>
            <?php if ($single->have_posts()) { ?> 

            <?php while ($single->have_posts()) : $single->the_post(); ?>

            <?php $postlang = $polylang->get_post_language(get_the_ID())->slug; ?>

            <?php if ($postlang == $lang) {
                get_template_part('project-thumb');
            } ?>


            <?php endwhile; ?>

            <?php } ?>
        <?php endforeach; ?>
        </ul>
    </div>


</div>


<?php get_footer() ?>

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.