You want to create a WordPress theme from scratch that shows a custom message only on the homepage and lists post titles on other pages. Which code snippet correctly implements this in index.php?
hard📝 component behavior Q15 of 15
Wordpress - Custom Theme Development
You want to create a WordPress theme from scratch that shows a custom message only on the homepage and lists post titles on other pages. Which code snippet correctly implements this in index.php?
A<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; else : ?>
<p>Welcome to my custom homepage!</p>
<?php endif; ?>
B<?php if ( is_front_page() ) : ?>
<p>Welcome to my custom homepage!</p>
<?php else : if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; endif; ?>
C<?php if ( is_home() ) : ?>
<p>Welcome to my custom homepage!</p>
<?php else : ?>
<h2>No posts to show</h2>
<?php endif; ?>
D<?php if ( is_front_page() ) : ?>
<p>Welcome to my custom homepage!</p>
<?php endif; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; ?>
Step-by-Step Solution
Solution:
Step 1: Use conditional tags correctly
is_front_page() checks if the current page is the homepage to show the custom message.
Step 2: Show posts only on other pages
Else block runs the loop to list post titles on non-homepage pages.
Step 3: Verify code structure
<?php if ( is_front_page() ) : ?>
<p>Welcome to my custom homepage!</p>
<?php else : if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; endif; ?> nests the else with have_posts() loop correctly, showing message only on homepage.
Final Answer:
The nested if-else code snippet -> Option B
Quick Check:
Conditional homepage message + posts elsewhere = <?php if ( is_front_page() ) : ?>
<p>Welcome to my custom homepage!</p>
<?php else : if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; endif; ?> [OK]
Quick Trick:Use is_front_page() for homepage, else loop for posts [OK]
Common Mistakes:
Putting posts loop inside homepage condition
Showing message only when no posts exist
Not separating homepage and other pages logic
Master "Custom Theme Development" in Wordpress
9 interactive learning modes - each teaches the same concept differently