skip to Main Content

I want to display all posts with the Projekty wnętrz category. Im using this query to achieve this, but it shows only few of posts (I added 8 and it shows only 6. I want to show always every post).

I have this problem with every category and it always shows only part of posts.

Thanks in advance 🙂

<div id="menu1" class="tab-pane fade">
     <div class="container">
        <div class="row">

            <?php $args = array( 'post_type' => 'Realizacje'); ?>
            <?php $loop = new WP_Query($args); ?>
            <?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); 
            $num = $loop->post_count; 
            $cat_id = get_cat_ID( 'Projekty wnętrz' );
            if (has_category( $cat_id )) {
            ?>
                <div class="col-md-4">
                    <div class="realisation">
                        <!--   <div class="gallery">
                     <?php 
                     $images = get_field('galeria');
                    $size = 'medium'; 
                    if( $images ): ?>
                        <div id="lightgallery<?php echo $count; ?>">
                        <?php foreach( $images as $image ): ?>
                            <div class="gal">
                                <img src="<?php echo $image; ?>"/>
                            </div>
                        <?php endforeach; ?>
                        </div>
                    <?php endif; ?>
                        </div> -->
                    <div class="galeria">
                        <div class="lightg" id="lightgallery<?php echo $count; ?>">
                                <a href="<?php echo get_the_post_thumbnail_url(); ?>">
                                    <div class="relative-box"><img class="first_image"src="<?php echo get_the_post_thumbnail_url(); ?>"/>
                                        <div class="demo-gallery-poster">
                                        <i class="fa fa-search" aria-hidden="true"></i>
                                        </div></div>
                                    <div class="gallery-title"><h5 class="gallery-t"><?php the_title(); ?></h5></div>
                                </a>
                                <?php 
                                $count=$count+1;
                                $images = get_field('galeria');
                                $size = 'large'; 
                                if( $images ): ?>
                                    <?php foreach( $images as $image ): ?>
                                        <a href="<?php echo $image; ?>"><img src="<?php echo $image; ?>"/></a> 
                                    <?php endforeach; ?>

                                <?php endif; ?>
                        </div>
                    </div>
                    </div>
                    </div>
                    <?php } ?>
                    <?php endwhile; ?>


                    <?php else: ?>
                        <h5>Brak realizacji</h5>
                    <?php endif; ?>
                    <?php wp_reset_postdata(); ?>
        </div>
    </div>
  </div>

2

Answers


  1. You’re not using any pagination parameters so it’s possible you’re getting only 10-12 posts from query based on how many posts per page value is set on backend settings.

    Then on top of that, you are using has_category( $cat_id ) to exclude the post content, You should not do this and should pass the category ID/slug in category parameters, You should not use the name, You must work with category ID or slug.

    Since you need all the posts from the category then you need to set posts_per_page to -1, and -1 will display all the posts with all the filters used in Query.

    the code will look like this after applying the above changes:

    <?php
    $args = array(
        'post_type'      => 'Realizacje',
        'posts_per_page' => -1,
        'cat'            => 101, // You need to change 101 with the category ID that you want to use.
    );
    /**
     * Note: If you want to use category slug then you will
     * have to use the 'category_name' param instead of using
     * 'cat' param and have to use category slug, not the name.
     * If you have to use multiple categories then you'll have 
     * to use the 'category__in' param instead of using the 'cat' param
     * 'category__in' will accept an array of category ids.
     */
    $loop = new WP_Query( $args );
    ?>
    
    Login or Signup to reply.
  2. Please check below full code. I’ve tested and it’s working fine.

    <div id="menu1" class="tab-pane fade">
     <div class="container">
        <div class="row">
    
            <?php $args = array( 
                'post_type' => 'Realizacje',
                'posts_per_page' => -1,
                'cat' => $cat_id //add category id if you want filter the post by category othrwise remove
            );
            $loop = new WP_Query($args);
            if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); 
            $num = $loop->post_count; 
            ?>
            <div class="col-md-4">
                <div class="realisation">
                    <div class="galeria">
                        <div class="lightg" id="lightgallery<?php echo $count; ?>">
                                <a href="<?php echo get_the_post_thumbnail_url(); ?>">
                                    <div class="relative-box"><img class="first_image"src="<?php echo get_the_post_thumbnail_url(); ?>"/>
                                        <div class="demo-gallery-poster">
                                        <i class="fa fa-search" aria-hidden="true"></i>
                                        </div></div>
                                    <div class="gallery-title"><h5 class="gallery-t"><?php the_title(); ?></h5></div>
                                </a>
                                <?php 
                                $count=$count+1;
                                $images = get_field('galeria');
                                $size = 'large'; 
                                if( $images ): ?>
                                    <?php foreach( $images as $image ): ?>
                                        <a href="<?php echo $image; ?>"><img src="<?php echo $image; ?>"/></a> 
                                    <?php endforeach; ?>
    
                                <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>
            <?php endwhile;
             else: ?>
            <h5>Brak realizacji</h5>
            <?php endif;
             wp_reset_postdata(); ?>
        </div>
    </div>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search