简体   繁体   中英

Select posts from Category - Wordpress

I am trying to filter posts from category.

This category is called "images"

And I have the code below:

<div id="owl-images" class="owl-carousel">
    <?php query_posts('category_name=images'); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
      <?php
      $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 5600,1000 ), false, '' );?>
      <a href='<?php echo $src[0]; ?>' data-lightbox="image-77" data-title="<?php the_title() ?>">
      <div class="item">
        <div class="lazyOwl" style="background:url(<?php echo $src[0]; ?>) no-repeat center center;background-size:cover;height:300px;" alt=""></div>
      </div>
    </a>
    <?php endwhile; endif; ?>
  </div>

The problem is that:

  • It is displaying some posts from other categories
  • It is not displaying all posts from category "image"

What is wrong with the code?

PS: I have seen this answer and it didn't help. Remained the same.

Best regards.

try like this

query_posts( array ( 'category_name' => 'images' ) ); 

you need to pass the slug of category image in this case.

You can also try with this as well

$catIDs = get_cat_ID('images');
query_posts("cat=$catIDs");

You should try with

cat=cat_id

instead of

category_name

For change Your default show posts per page:

Go to setting -> reading -> Blog pages

使用category__in并传递您的类别ID

<?php query_posts('category__in=cat_id'); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM