WordPress: How to limit number of attachments

I’m using Sweet Cake theme. As you can see the portfolio section show all pictures. I want when someone load the site to be displayed items of the first category , instead of everything. How can I do that?

 <!--start all categories-->
                <?php
                $args=array(
                    'type' => 'attachment',
                    'child_of'                 => 0,
                    'orderby'                  => 'name',
                    'order'                    => 'ASC',
                    'hide_empty'               => 0,
                    'hierarchical'             => 1,
                    'post_status'              => 'inherit',
                    'taxonomy'                 => 'portfolio'
                );
                $categories=get_categories($args);

                foreach($categories as $category) { ?>
                    <li><a style="background-color:<?php echo $category->description; ?>;" href="#filter" data-option-value=".<?php echo $category->slug; ?>"><?php echo $category->name ?></a></li>
                <?php } ?>
                <!--end all categories-->

            </ul>
        </div>
    </div>
    <!--end options-->

    <!--start images-->
    <div id="containerisotope" class="clear">

        <?php
        foreach($categories as $category) {
            $args = array(
                'post_type' => 'attachment',
                'posts_per_page' => -1,
                'post_status' => 'inherit',
                'tax_query' => array(
                    array(
                        'taxonomy' => 'portfolio',
                        'field' => 'slug',
                        'terms' => $category -> slug
                    )
                )
            );


            $attachments = get_posts($args);
            if ($attachments) {
                foreach ( $attachments as $attachment ) { ?>

                    <?php
                    $id = $attachment->ID;
                    $big_image = wp_get_attachment_image_src($id, '');
                    $thumb_image = wp_get_attachment_image_src($id, 'portfolio-thumbnail');
                    $alt = get_post_meta($id, '_wp_attachment_image_alt', true);
                    $image_title = $attachment->post_title;
                    $caption = $attachment->post_excerpt;
                    $description = str_replace('"', ''',$attachment->post_content);
                    ?>

                    <!--element-->
                    <div class="element <?php echo $category -> slug; ?>" data-category="<?php echo $category -> slug; ?>">

                        <!--start check if alt is declared-->
                        <?php if (!empty($alt)) { ?>
                            <a href="<?php echo $alt; ?>">
                        <?php } else { ?>
                            <a class="alttooltip" title="<?php echo $description; ?>" data-rel="prettyPhoto[]" href="<?php echo $big_image[0]; ?>"> 
                        <?php } ?>
                        <!--end if-->

                            <img alt="<?php echo $alt; ?>" class="imgwork" src="<?php echo $thumb_image[0]; ?>" />
                        </a>
                        <div class="worksarrow">
                            <img alt="" src="<?php echo get_template_directory_uri(); ?>/img/section-works/arrow.png" />
                        </div>
                        <h2><?php echo $image_title; ?></h2>
                        <p><?php echo $caption; ?></p>

                        <div class="worksbottom"></div>
                    </div>
                    <!--element-->

                <?php } ?>
            <?php } ?>
        <?php } ?>


    </div>
    <!--end images-->

Related posts

Leave a Reply