A
A
Andress842021-06-23 17:15:51
WordPress
Andress84, 2021-06-23 17:15:51

How to display a certain number of posts from a category (taxonomy) in WordPress?

Good time, I use Isotope filter on the site. I registered my post types and taxonomy, but I don’t understand how to display a certain number of posts from a category (taxonomy). That is, to each category was up to 8 posts.

<div class="isotope-nav" data-isotope-nav="isotope">
            <ul>
                <?php
                $terms = get_terms( [
                    'taxonomy' => 'work-cases',
                    'hide_empty' => false,
                ] );

                foreach ($terms as $term) {
                    echo '<li><a href="javascript:" data-filter=".'.$term->slug.'">'.$term->name.'</a></li>';
                }
                ?>
            </ul>
</div>


<div id="isotope" class="isotope">

        <?php
        // параметры по умолчанию
        $args = array(
            'numberposts' => 8,
            'order'       => 'DESC',
            'post_type'   => 'portfolio',
            'suppress_filters' => true, // подавление работы фильтров изменения SQL запроса
        );
        $posts = get_posts( $args );
        foreach( $posts as $post ){ setup_postdata($post);
            ?>
            <div class="isotope-item <?php istope_classes(get_the_id()); ?>">
                <div class="grid">
                    <a href="<?php echo get_permalink(); ?>">
                        <figure class="effect-oscar">
                            <img src="<?php
                            if ( has_post_thumbnail() ) {
                                the_post_thumbnail_url();
                            } else {
                                echo  get_template_directory_uri() . '/assets/images/tile-default.jpg';
                            }
                            ?>" alt="">
                            <figcaption>
                                <h3><?php the_title(); ?></h3>
                                <div class="link link-ico icon-pentagon"><i class="fa fa-search"></i></div>
                            </figcaption>
                        </figure>
                    </a>
                </div>
            </div><!-- Isotope item end -->
            <?php
        }

        wp_reset_postdata(); // сброс
        ?>

 </div><!-- Isotope content end -->

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question