0

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>

It is inserting all the images attached as a background. It is working but not properly. Sometimes posts that aren't in the 'images' category are displayed so I guess there is something wrong but I don't know much of Wordpress development.

Best regards.

2

In my opinion, you should avoid using common words like "images". It can be confusing for the system.

A previous post on SO nailed the use of images using Category ID. Here's your edited code:

<?php 
$your_category_ID = '1'      

$query = new WP_Query( 'cat=$your_category_ID' );

if ( $query->have_posts() ) {

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

        $src = wp_get_attachment_image_src( get_post_thumbnail_id($query->ID), array( 5600,1000 ), false, '' ); ?>
        <a href='<?php echo $src[0]; ?>' data-lightbox="image-77" data-title="<?php query->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>

    endwhile; 

    /* Restore postdata */
    wp_reset_postdata();    

}
?>
  • I wish I could comment the question but I don't have the privilege to comment yet. I'll edit the answer. Thank you for the tip. – Axel De Acetis Dec 1 '14 at 15:15
  • 1
    Personaly, I always use WP_Query. I didn't know the difference between them. I'll edit my answer again. Thank you for the welcome advice. ;) – Axel De Acetis Dec 1 '14 at 15:43
  • Let us continue this discussion in chat. – Axel De Acetis Dec 2 '14 at 11:19
0

Use a category number instead of name...

i.e. query_posts('cat=1');

Looks like you have some lightbox code mixed in with your query. Not sure what exactly you are trying to do. If you don't want to use the images as bg images get rid of the...

<div class="lazyOwl" style="background:url(<?php echo $src[0]; ?>) no-repeat center center;background-size:cover;height:300px;" alt="">

and try an img tag instead.

<img src="<?php echo $src[0]; ?>" />

also good to end your query...

wp_reset_query();

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.