1

I have this snippet in my header.php to display a slider:

    <?php
    $args = array(
        'post_type'             => 'post',
        'posts_per_page'        => 6,
        'meta_query'            => array(
            array(
                'key'           => '_thumbnail_id',
                'compare'       => 'EXISTS'
            ),
        )
    );
    $posts = get_posts( $args );

    global $post;
    foreach ( $posts as $post ) {
        setup_postdata( $post );
    ?>
    <div class="immtestata">
        <div>
            <span><a href="<?php echo esc_url( home_url() ); ?>/category/<?php $category = get_the_category(); echo $category[0]->category_nicename; ?>"><?php $category = get_the_category(); echo $category[0]->cat_name; ?></a></span>
             - 
             <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </div>
        <img style="width:1000px;height:288px" src="<?php $src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID) , array(1000, 288) , false, ''); echo $src[0]; ?>" />
    </div>
    <?php
    }
        wp_reset_postdata();
    ?>

But it conflicts with my content.php as the_content() no longer displays the actual post content but the latest recent post! I can't figure out why!

No conflict instead with query_posts (but I know it shouldn't be used).

    <?php
    $args = array(
        'post_type'             => 'post',
        'posts_per_page'        => 6,
        'meta_query'            => array(
            array(
                'key'           => '_thumbnail_id',
                'compare'       => 'EXISTS'
            ),
        )
    );
    query_posts( $args );
    while ( have_posts() ) : the_post();
    ?>
    <div class="immtestata">
        <div>
            <span><a href="<?php echo esc_url( home_url() ); ?>/category/<?php $category = get_the_category(); echo $category[0]->category_nicename; ?>"><?php $category = get_the_category(); echo $category[0]->cat_name; ?></a></span>
             - 
             <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </div>
        <img style="width:1000px;height:288px" src="<?php $src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID) , array(1000, 288) , false, ''); echo $src[0]; ?>" />
    </div>
    <?php
    endwhile;

    wp_reset_query();
    ?>

Why is this happening with get_posts?

  • Avoid using query_posts() and use WP_Query() instead. (Be learned) – Mayeenul Islam Dec 28 '14 at 4:59
  • It looks like we better use something else than $posts, for example $myposts ;-) The WordPress globals can sometimes mess things up ;-) I wish WordPress would use a prefix for it's own globals. – birgire Dec 28 '14 at 8:17
  • @birgire You should post that as an answer. :-) – Pieter Goosen Dec 28 '14 at 13:42
  • I'll try soon if it works – MultiformeIngegno Dec 28 '14 at 13:43
  • 1
    @PieterGoosen ok, but let's see what the OP says after his own check ;-) – birgire Dec 29 '14 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.