samedi 27 décembre 2014

Conflict with get_posts and the_content


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?





Aucun commentaire:

Enregistrer un commentaire