0
0
Wordpressframework~30 mins

The Loop and query in Wordpress - Mini Project: Build & Apply

Choose your learning style9 modes available
The Loop and query
📖 Scenario: You are building a simple WordPress theme template that shows a list of blog posts. You want to display only posts from the category "News".
🎯 Goal: Create a WordPress Loop that queries posts from the category "News" and displays their titles in an unordered list.
📋 What You'll Learn
Create a query to get posts from the category "News"
Use The Loop to display the post titles
Wrap the titles in an unordered list
Reset the post data after the loop
💡 Why This Matters
🌍 Real World
WordPress themes often need to show posts from specific categories or with custom filters. This project teaches how to do that safely and clearly.
💼 Career
Understanding The Loop and WP_Query is essential for WordPress developers to customize themes and build dynamic content displays.
Progress0 / 4 steps
1
Create the initial query
Create a variable called $news_query that uses new WP_Query to get posts from the category slug 'news'.
Wordpress
Need a hint?

Use new WP_Query(array('category_name' => 'news')) to get posts from the "News" category.

2
Check if the query has posts
Add an if statement that checks $news_query->have_posts() to see if there are posts to show.
Wordpress
Need a hint?

Use if ($news_query->have_posts()) to check if posts exist.

3
Create The Loop to display post titles
Inside the if block, start an unordered list with <ul>. Then use a while loop with $news_query->have_posts() and inside it call $news_query->the_post(). For each post, add a list item <li> with the post title using get_the_title(). Close the unordered list after the loop.
Wordpress
Need a hint?

Use while ($news_query->have_posts()) { $news_query->the_post(); } and inside echo list items with get_the_title().

4
Reset post data after the loop
After the if block, call wp_reset_postdata() to restore the original post data.
Wordpress
Need a hint?

Call wp_reset_postdata() after the loop to clean up.