Categories
Wordpress

Le Loop WordPress

Le Loop WordPress et la requète query_post():

Permet d’afficher le/les posts.

[fusion_builder_container hundred_percent=”yes” overflow=”visible”][fusion_builder_row][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][php]
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<!– do stuff … –>
<?php endwhile; ?>
<?php endif; ?>
[/php]


The have_posts() simply calls into $wp_query have_posts() which checks a loop counter to see if there are any posts left in the post array.
And the_post() calls $wp_query the_post()

Avant le Loop, on peut utiliser la fonction query_posts()

Exemple: Selectionne les posts de la catégorie 8 uniquement.

//The Query

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][php]
<?php query_posts(‘<span style="color: #339999;">cat=8</span>’); ?>[/php]

ou

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][php]<?php query_posts(‘<span style="color: #339999;">category_name=categorie1</span>’); ?>[/php]

//The Loop

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][php]
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>
<?php endwhile; ?>
<?php endif; ?>[/php]

Exemple: Exclure les posts d’une catégorie.

//The Query

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][php]<?php query_posts(‘<span style="color: #339999;">cat=-3</span>’): ?>[/php]

//The Loop

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][php]<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>
<?php endwhile; ?>
<?php endif; ?>[/php]

Exemple : Afficher un article particulier grâce à son id

Ici, 4 étant l’ID du post

//The Query

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][php]<?php query_posts(‘<span style="color: #339999;">p=4</span>’); ?>[/php]

//The Loop

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][php]<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post ; ?>
<?php endwhile; ?>
<?php endif; ?>[/php]

Exemple : Afficher une page en particulier grâce à son id

Ici, 6 est l’ID du post

//The Query

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][php]<?php query_posts(‘<span style="color: #339999;">page_id=6</span>’); ?>[/php]

//The Loop

Le Loop ici

Exemple : Gère le nombre posts à afficher

Taper 10 pour afficher 10 posts. Taper -1 pour afficher tous les posts

//The Query

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][php]<?php query_posts(‘<span style="color: #339999;">posts_per_page=10</span>’); ?>[/php]

//The Loop

Le Loop ici

Exemple : Ordre ascendant et catégorie 6 uniquement

//The Query

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][php]<?php query_posts(‘<span style="color: #339999;">order=ASC&amp;cat=6</span>’); ?>[/php]

//The Loop

Le Loop ici

Autres Exemples:

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][php]query_posts(‘cat=2,6,17,38’);// 2 OU 6 OU 17 OU 38[/php]

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][php]query_posts(array(‘category__in’ => array(2,6)));//idem 2 OU 6[/php]

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][php]query_posts(array(‘category__and’ => array(2,6)));// 2 ET 6[/php]

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][php]query_posts(array(‘category__not_in’ => array(2,6)));[/php]

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][php]query_posts(array( ‘post_type’ => array(‘post’, ‘movie’) ) );[/php]

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][php]query_posts(‘author=1&amp;post_type=page&amp;post_status=publish&amp;orderby=title&amp;order=ASC’);[/php]

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][php]query_posts(‘monthnum=12&amp;day=20’);// Affiche les posts du 20 décembre uniquement[/php]

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][php]query_posts(‘posts_per_page=5&amp;offset=1’);Affiche 5 posts à la suite du plus récent.[/php]

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][php]query_posts(‘cat=3&amp;year=2010’);Dans la catégorie 3 de l’année 2010[/php]

Autres manières d’utiliser query_posts:

///////////////////////////

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][php]query_posts(array(
‘cat’ => 22,
‘year’ => $current_year,
‘monthnum’ => $current_month,
‘order’ => ‘ASC’,
));[/php]

///////////////////////////

//The Query

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][php]<?php
$query = ‘posts_per_page=10’;
$queryObject = new WP_Query($query);[/php]

// The Loop

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][php]if ($queryObject->have_posts()) {
while($queryObject->have_posts()) {
$queryObject->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php
the_content();
}
}
?>[/php]

////////////////////////

//The Query

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][php]<?php query_posts($query_string . ‘&amp;cat=-3,-8’); ?>[/php]

//The Loop

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][php]<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<!– Display the Title as a link to the Post’s permalink. –>
<a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?><a>

<!– Display the date (November 16th, 2009 format) and a link to other posts by this posts author. –>
<?php the_time(‘F jS, Y’) ?> by <?php the_author_posts_link(); ?>
<?php the_content(); ?>

Posted in <?php the_category(‘, ‘); ?>

<?php endwhile; else: ?>

Sorry, no posts matched your criteria.

<?php endif; ?>[/php]

Si un problème survient avec la pagination:

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][php]<?php $temp = $wp_query; ?>
<?php $wp_query= null; ?>
<?php $wp_query = new WP_Query(); ?>
<?php $wp_query->query(‘category_name=artigo&amp;showposts=5′.’&paged=’.$paged); ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>[/php]

et voir ce lien: http://scribu.net/wordpress/wp-pagenavi/right-way-to-use-query_posts.html

Sources:

http://codex.wordpress.org/Template_Tags/query_posts
http://www.tipsandtricks-hq.com/query-or-show-a-specific-post-in-wordpress-php-code-example-44
http://www.blogherald.com/2007/05/31/a-wordpress-developers-best-friend/
http://www.fran6art.com/webdesign/wordpress-query_posts-la-requete-a-tout-faire/
http://www.binarymoon.co.uk/2010/03/5-wordpress-queryposts-tips/[/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]

2 replies on “Le Loop WordPress”

Leave a Reply

Your email address will not be published. Required fields are marked *