Complete the code to start The Loop in WordPress.
<?php if ( have_posts() ) : while ( [1] ) : the_post(); ?> <h2><?php the_title(); ?></h2> <?php endwhile; endif; ?>
The Loop uses have_posts() in the while condition to check for posts and the_post() to set up each post inside the loop.
Complete the code to create a custom query for posts in WordPress.
<?php $custom_query = new WP_Query( array( 'category_name' => [1] ) ); ?>
The category name must be a string inside quotes, so use 'news'.
Fix the error in the code to properly reset post data after a custom query.
<?php $query = new WP_Query(); /* custom query code */ wp_reset_[1](); ?>After a custom WP_Query loop, use wp_reset_postdata() to restore the original post data.
Fill both blanks to create a loop that shows only 5 posts from a custom query.
<?php $my_query = new WP_Query( array( 'posts_per_page' => [1] ) ); if ( [2] ) : while ( $my_query->have_posts() ) : $my_query->the_post(); ?> <h2><?php the_title(); ?></h2> <?php endwhile; endif; wp_reset_postdata(); ?>
Set 'posts_per_page' to 5 and check the custom query with $my_query->have_posts().
Fill all three blanks to create a custom query that fetches posts from category 'events' and orders them by date descending.
<?php $events_query = new WP_Query( array( 'category_name' => [1], 'orderby' => [2], 'order' => [3] ) ); if ( $events_query->have_posts() ) : while ( $events_query->have_posts() ) : $events_query->the_post(); ?> <h2><?php the_title(); ?></h2> <?php endwhile; endif; wp_reset_postdata(); ?>
Use 'events' for category_name, order by 'date', and order descending with 'DESC'.