approaching theme development….

  • Things covered today
  • Where to begin

Things Covered Today


The loop:

  • We don’t have to query again to get posts because the main query almost always has the data we require.
  • To loop through this we use below code
<?php
if(have_posts()){
  while(have_posts()){
the_post() //update the global post
echo the_title();
} //endwhile
}else{
echo "Sorry, no post found";
}

Why the the_post() is required ? well I forgot to call it and the while loop become infinite. This function sets up the next post in the loop so if we don’t call it then we will go into infinite loop.

Can we have multiple loops?

  • Yes, on a page we may have a use case to create multiple loops to access the posts like on home page we want to show trending and upcoming both posts. For that we can use multiple loops and also custom query.
  • Here is a good example of how to use multiple loops:-
<?php
// Start the main loop
if ( have_posts() ) :
    while ( have_posts() ) : the_post();
        the_title();
    endwhile;
endif;

// Use rewind_posts() to use the query a second time.
rewind_posts();

// Start a new loop
while ( have_posts() ) : the_post();
    the_content();
endwhile;
?>

! It is important that we reset the posts after each loop, without it will give unexpected results.

Here are 3 ways we can reset the post query
  1. using rewind_posts()
  2. using wp_reset_postdata()
  3. using wp_reset_query()

Template tags that can be used inside loop

the_author()
the_category()
the_content()
the_excerpt()
the_ID()

Common Conditional Tags for templates

is_home()
is_admin()
is_page()
is_single()
is_author()

Well we are almost 10% there to start building our theme so let’s begin.

Where to begin?


We are now equipped with some knowledge on how themes work in WordPress and now it is time to build one for our assignment.

For people like me who don’t like fron-tend much here’s how I apporach:-

  • Look at the codebase of themes already built
    • the twentytwnety theme series is a good start to look at how the themes code look like
  • Start with just a button
    • Sometimes we hate to do something because we haven’t tried it for long enough.
    • Start with just a button, make it to your liking and build from there.
  • Start writing HTML
    • Just start writing some HTML, use the template tags and conditionals in WordPress to maybe just list the titles of the post and take it further from there.

For reference Here’s how my index.php and style.css looks now:

<?php
while (have_posts()){
	the_post();
	the_post_thumbnail([40,40]);
	the_title('<h2>', '</h2>');
	the_excerpt();

}
?>
/*
Theme Name: Screen Time
Description: Theme for managing movies.
 */

body{
    background-color: rgb(255,105,180);
    color: black;
    margin: 0px;
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *