WordPress Loop - Using wp_query and parameters for custom loop
<?php
//https://codex.wordpress.org/Class_Reference/WP_Query#Parameters
//wp_reset_postdata();
//couple of parameters in example
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
);
$the_query = new WP_Query($args);
//wp_reset_postdata();
<?php
get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
<?php
//basic WordPress Loop
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h3><?php the_title() ;?></h3>
<?php the_content(); ?>
<?php endwhile; else: ?>
<p>No content available</p>
<?php endif; ?>
<?php
//for custom loops use WP_Query
<?php
//example of regular loop followed by wp_query with parameters followed by new loop
get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
<? wp_reset_postdata(); //resets loop
//new wp_query function and parameters
//https://codex.wordpress.org/Class_Reference/WP_Query#Parameters - all the parameters
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
);
$the_query = new WP_Query($args);
//wp_reset_postdata(); // not need reset is after initial loop
//newloop ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); //Applying only the new paraneters $the_query-> ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php
//example of regular loop followed by wp_query with parameter that displays posts with the wordpress in it, followed by new loop
get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
<? wp_reset_postdata(); //resets loop
//new wp_query function and parameters
//https://codex.wordpress.org/Class_Reference/WP_Query#Parameters - all the parameters
$args = array(
's' => 'wordpress',
);
$the_query = new WP_Query($args);
//wp_reset_postdata(); // not need reset is after initial loop
//newloop ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); //Applying only the new paraneters $the_query-> ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->