WordPress have_posts()

WordPress have_posts() functions is used to check the current wordpress query has any data to loop. It will return true If data is available. Otherwise it will return false. Note: We dont need send any arguments for this function.

Format 1:

It will basic format in wordpress archieve page, category page etc.

<?php
if ( have_posts() ):		
while ( have_posts() ): the_post();

	// Process Your Statements        
		
endwhile;
endif;
?>

Format 2:

Custom query to have_post() function in wordpress.

<?php
$args = array(
	'post__not_in'=>array($array),
    'post_type' => 'post',
    );
	
$the_query = new WP_Query($args);

if($the_query->have_posts()):

while ($the_query->have_posts()) : $the_query->the_post();
	// Process Your Statements
	//ex the_content()
	echo the_title();
endwhile;

endif;
?>

Format 3:

<?php
$args = array(
	'post__not_in'=>array($array),
    'post_type' => 'post',
    );
	
$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ) {
	while ( $the_query->have_posts() ) {
		$the_query->the_post();
		echo get_the_title();
	}
}
?>

Format 4:

<?php 
//Syntax: query_posts(CONDITION)

query_posts('category_name=student&posts;_per_page=10'); 

while ( have_posts() ): the_post();
		// Process Your Statements
endwhile;
?>

 

Leave a Reply

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