DEV Community

Ruxin Qu
Ruxin Qu

Posted on

wordpress: the loop

  1. use Local for dev development. The theme files are saved in the path: /Local Sites/site-name/app/public/wp-content/themes/theme-name/.
  2. index.php is normally used as a homepage that display all the posts and single.php displays the single post.
  3. php the loop:
while(have_posts()){
the_post() 
}
Enter fullscreen mode Exit fullscreen mode

the_post()function keeps track of the current post.
When reaching the loop, Wordpress first determines if the page is single.php or not. If not, Wordpress retrieves multiple posts based on the requested criteria. If the loop is in the single.php page, Wordpress will only retrieve the current post.

  1. using html in php functions, use ?> <html here> <?php can display the post data retrieved from Wordpress database to the page. For example: ?><h2><?php the_title()?></h2><?php displays the title of the post
  2. adding a new page: go to the admin and click 'add new page'. The url will be shown on page setting. Update the page template in page.php file.
  3. page.php displays the template that will apply to all pages except the home and single page. To do conditional rendering, use if statement. For example:
if(is_page('test'){
?><h1>This is a test page</h1><?php
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)