0
0
Wordpressframework~10 mins

The Loop and query in Wordpress - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start The Loop in WordPress.

Wordpress
<?php if ( have_posts() ) : while ( [1] ) : the_post(); ?>
  <h2><?php the_title(); ?></h2>
<?php endwhile; endif; ?>
Drag options to blanks, or click blank then click option'
Athe_post()
Bhave_posts()
Cget_posts()
Dquery_posts()
Attempts:
3 left
💡 Hint
Common Mistakes
Using the_post() inside the while condition instead of have_posts()
Using get_posts() which returns an array, not suitable for The Loop
Using query_posts() which modifies the main query and is not used inside The Loop
2fill in blank
medium

Complete the code to create a custom query for posts in WordPress.

Wordpress
<?php $custom_query = new WP_Query( array( 'category_name' => [1] ) ); ?>
Drag options to blanks, or click blank then click option'
A'news'
Bnews
C'post'
Dcategory
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the category name
Using the wrong key like 'category' instead of 'category_name'
3fill in blank
hard

Fix the error in the code to properly reset post data after a custom query.

Wordpress
<?php $query = new WP_Query(); /* custom query code */ wp_reset_[1](); ?>
Drag options to blanks, or click blank then click option'
Aquery
Bpost
Cloop
Dpostdata
Attempts:
3 left
💡 Hint
Common Mistakes
Using wp_reset_query() which resets the main query but not post data
Using wp_reset_loop() which does not exist
4fill in blank
hard

Fill both blanks to create a loop that shows only 5 posts from a custom query.

Wordpress
<?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(); ?>
Drag options to blanks, or click blank then click option'
A5
B$my_query->have_posts()
Chave_posts()
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Using the global have_posts() instead of the custom query's method
Setting posts_per_page to a string instead of a number
5fill in blank
hard

Fill all three blanks to create a custom query that fetches posts from category 'events' and orders them by date descending.

Wordpress
<?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(); ?>
Drag options to blanks, or click blank then click option'
A'events'
B'date'
C'DESC'
D'title'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ASC' instead of 'DESC' for descending order
Using category slug without quotes
Using 'title' instead of 'date' for orderby