Skip to content
Menu
i do, therefore i am
  • Home
  • Blog
  • Contact us
i do, therefore i am
October 20, 2022November 7, 2022

A WordPress theme from scratch 4

Content. Custom Classes.

We have added menus capabilities to our webpage and that is excellent. But where is the beef, or more specifically, where are the posts and the pages and everything else: where is the content.

To access the content, we need to create a content-loop. On WordPress, this is also known as The Loop. The basic structure is:

<?php
//begin The Loop
if(have_posts()){
  while(have_posts()){
    the_post();
    //
    //post content here
    //
  }
} 
?>

We place the loop in our index.php and show some post information. Our index.php now looks like this:

<?php get_header(); ?>
<?php
if (have_posts()){
  while(have_posts()){
    the_post();
?>
<h3><?php the_title();?></h3>
<?php the_content();?>
created on <?php the_time('F j,Y');?> at <?php the_time();?> in <?php the_category(); 
    } 
  }
?>
<?php get_footer(); ?>

Custom body classes

We can customize the css-class of the body by adding calling the WordPress function body_class() within our start body-tag. Since we put the opening body tag in the file header.php, we need to edit there also:

<!doctype html>
  <html>
    <head>
      <title>my-first-thema</title>
      <?php
      /*Insert qeued content (css) here.
      See functions.php*/ 
        wp_head(); 
       // wp_head(); 
      ?>
    </head>
    <body <?php body_class();?>>
      <?php
      wp_nav_menu(array('theme_location'=>'primary'));
      ?>

The above pictures show the generated HTML before and after calling body_class(). In addition to the automatic generated classes, we can pass an array of custom classes to body_class() as follows:

<body <?php body_class(array('my_custom_bodyclass1','my_custom_bodyclass2'));?>>

The result is then:

These dynamic generated classes allow us to style our pages differently according to their content.

More customizing

We can go one step further in customizing this body classes. WordPress provides functions like is_home() which return true for the page that show your posts, or is_front_page() which returns true for the Homepage. By calling these functions, we can apply different classes to specific pages.

<!doctype html>
  <html>
    <head>
      <title>my-first-thema</title>
      <?php
      /*Insert qeued content (css) here.
      See functions.php*/ 
        wp_head(); 
       // wp_head(); 
      ?>
    </head>
    <?php
      if (is_home()){
        $custom_classes = array('my_blog_class','my_other_blog_class');
      }elseif (is_front_page()){
        $custom_classes = array('my_home_class','my_other_home_class');
      }else{
        $custom_classes = array('my_not_so_custom_class');
      }
    ?>
    <body <?php body_class($custom_classes);?>>
      <?php
      wp_nav_menu(array('theme_location'=>'primary'));
      ?>

In this example, we apply the classes my_blog_class and my_other_blog_class to the page that show the blog (is_home()), apply the classes my_home_class and my_other_home_class to the Homepage (is_front_page()) and apply the class my_not_so_custom_class to everything else.

https://codex.wordpress.org/The_Loop

https://www.youtube.com/watch?v=pJ4NTBdvyj4&list=PLriKzYyLb28nUFbe0Y9d-19uVkOnhYxFE&index=4

Leave a Reply Cancel reply

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

Recent Posts

  • vim as Hex-Editor
  • installing pico-sdk in wsl
  • A WordPress theme from scratch 5
  • A WordPress theme from scratch 4
  • Serial Port Windows

Recent Comments

    Archives

    • May 2023
    • January 2023
    • November 2022
    • October 2022
    • September 2022
    • May 2022
    • April 2022
    • March 2022
    • February 2022
    • January 2021

    Categories

    • Fortran
    • linux
    • Postgresql
    • Programming
    • Python
    • Tools, tips and tricks
    • Wordpress

    Meta

    • Log in
    • Entries feed
    • Comments feed
    • WordPress.org

    Postgres

    • postgres-docs

    Python

    • data model
    • the standard library
    • python tips
    • import system
    • asyncio
    • built-in functions

    Open_VMS

    • lexicals at marc’s place
    • documentation and manuals
    • system services
    • rms services reference
    ©2025 i do, therefore i am | Powered by WordPress and Superb Themes!