1

I'm having a custom type called portfolio with different categories. I want to display all my categories except one. I only want to show one item per category on my page.

I'm been busting my butt on this and i only get my loop to show all the items in the category but can't get only one item per category. So far i get to display the categories, but they repeat itself, i can't get it to show just once on the page. One category per page.

here is the two loops i tried.

Loop1

$args = array (
        'post_type' => 'portfolio',
        'category__not_in' => array(28,30),
    );

    $newQuery = new WP_Query($args);

    if ($newQuery -> have_posts() ):
        while( $newQuery-> have_posts() ): $newQuery-> the_post();
            echo get_the_term_list( $post->ID , 'portfolio_type'); 
        endwhile;
    endif;

    wp_reset_postdata();

Test loop 2

$args_cat = array (
        'taxonomy' => 'portfolio_type',
        'exclude' =>  '28,30',
    );
    $categories = get_categories($args_cat);

    foreach($categories as $category):

        $args = array (
            'post_type' => 'portfolio',
            'posts_per_page' => 1,
        );

        $newQuery = new WP_Query($args);

        if ($newQuery -> have_posts() ):
            while( $newQuery-> have_posts() ): $newQuery-> the_post();

                echo get_the_term_list( $post->ID , 'portfolio_type'); 

            endwhile;
        endif;

        wp_reset_postdata();
    endforeach;

Can anyone help me thanks?!

1
$args = array(
                    'taxonomy' => 'portfolio_type',
                    'exclude' => '28,30',
                    );
                $tax = get_terms($args);
                foreach($tax as $t){




                    echo '<div class="span3 produto-lista">';

                        $args = array(

                                  'tax_query' => array(
                                    array(
                                        'taxonomy' => 'portfolio_type',
                                        'field' => 'slug',
                                        'terms' => array($t->slug)
                                    )
                                ),
                                  'posts_per_page' => 1,
                                  'post_status' => 'publish',
                                  'order' => 'DESC',

                                  );
                        $query = new WP_Query( $args );




                        while ( $query->have_posts() ) {
                            $query->the_post();

                            ?>

                            <a href="<?php echo get_term_link($t); ?>">

                            <?php
                            the_post_thumbnail(); ?>
                            <h1>
                            <?php
                            echo $t->name; 
                            ?>
                            </h1>
                            </a>
                            <?php
                        }
                        wp_reset_query();
                    echo '</div>';
                }

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.