Na pocetnoj stranici sajta koristim 3 query-ja, prvi za slajder, drugi za carousel i treci za sadrzaj pocetne stranice.
Koju god metodu koristio za kreiranje loop-a(WP_Query, get_posts ili query_posts), carousel prikazuje duple rezultate(trebalo bi da prikaze 3, medjutim on prikazuje 6, kao da dva puta prolazi kroz query- 1, 2, 3, 1, 2, 3).
Kod za prvi query(radi ocekivano):
Code:
<ul class="slides">
<?php
$args = array (
'post_type' => 'slide',
);
$the_query = new WP_Query( $args );
if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php
$thumb_id = get_post_thumbnail_id();
$thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail-size', true);
?>
<li><?php echo '<a href="' . get_field( "slide_link" ) . '"><img src="' . $thumb_url[0] . '" /></a>' ?></li>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
<?php wp_reset_postdata(); ?>
</ul>
<ul class="slides">
<?php
$args = array (
'post_type' => 'slide',
);
$the_query = new WP_Query( $args );
if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php
$thumb_id = get_post_thumbnail_id();
$thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail-size', true);
?>
<li><?php echo '<a href="' . get_field( "slide_link" ) . '"><img src="' . $thumb_url[0] . '" /></a>' ?></li>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
<?php wp_reset_postdata(); ?>
</ul>
Kod za drugi query(prikazuje duple rezultate):
Code:
<ul class="amazingcarousel-list">
<?php
$posts = get_posts(array(
'post_type' => array('post', 'page', 'featured_rides'),
'meta_query' => array(array(
'key' => 'display_this_entry_inside_homepage_carousel',
'value' => '1',
'compare' => '=='
)
)
));
if( $posts ): ?>
<?php foreach( $posts as $post ):
setup_postdata( $post )
?>
<li class="amazingcarousel-item">
<div class="amazingcarousel-item-container">
<div class="amazingcarousel-image"><a href="<?php echo get_permalink(); ?>"><?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?></a></div>
<div class="amazingcarousel-title"><a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a></div></div>
</li>
<?php endforeach; ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
<?php wp_reset_query(); ?>
</ul>
<ul class="amazingcarousel-list">
<?php
$posts = get_posts(array(
'post_type' => array('post', 'page', 'featured_rides'),
'meta_query' => array(array(
'key' => 'display_this_entry_inside_homepage_carousel',
'value' => '1',
'compare' => '=='
)
)
));
if( $posts ): ?>
<?php foreach( $posts as $post ):
setup_postdata( $post )
?>
<li class="amazingcarousel-item">
<div class="amazingcarousel-item-container">
<div class="amazingcarousel-image"><a href="<?php echo get_permalink(); ?>"><?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?></a></div>
<div class="amazingcarousel-title"><a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a></div></div>
</li>
<?php endforeach; ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
<?php wp_reset_query(); ?>
</ul>
Interesantno - ako za post_type izaberem samo jedan(npr 'post_type' => 'post') query radi ocekivano(bez duplikata)
Treci query zapravo i nije query, koristim 'the_content()' funkciju za prikaz sadrzaja pocetne strane.
Unapred zahvalan :)