style.css
index.php
A WordPress theme is a collection of files that work together to define the look and feel of a WordPress website. It includes templates, stylesheets, and other resources that control how the content is displayed on the front - end of the site.
You can use tools like XAMPP (for Windows) or MAMP (for Mac) to set up a local development environment. Follow these steps:
htdocs
folder (for XAMPP) or htdocs
or www
folder (for MAMP).http://localhost/wordpress
in your browser and follow the installation wizard.wp - content/themes
directory in your WordPress installation.my - custom - theme
.style.css
The style.css
file is the main stylesheet for your theme. It also contains metadata about the theme, such as the theme name, author, and description. Here is an example:
/*
Theme Name: My Custom Theme
Theme URI: http://example.com
Author: Your Name
Author URI: http://yourwebsite.com
Description: A custom WordPress theme.
Version: 1.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl - 2.0.html
Text Domain: my - custom - theme
*/
/* Global styles */
body {
font-family: Arial, sans-serif;
line-height: 1.6;
margin: 0;
padding: 0;
}
index.php
The index.php
file is the main template file for your theme. It is used to display the content of the website. Here is a basic example:
<?php
/**
* The main template file.
*
* This is the most generic template file in a WordPress theme
* and one of the two required files for a theme (the other being style.css).
* It is used to display a page when nothing more specific matches a query.
* E.g., it puts together the home page when no home.php file exists.
*
* @link https://developer.wordpress.org/themes/basics/template - hierarchy/
*
* @package my - custom - theme
*/
get_header();
?>
<div id="primary" class="content - area">
<main id="main" class="site - main">
<?php
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
get_template_part( 'template - parts/content', get_post_type() );
endwhile;
the_posts_navigation();
else :
get_template_part( 'template - parts/content', 'none' );
endif;
?>
</main><!-- #main -->
</div><!-- #primary -->
<?php
get_sidebar();
get_footer();
You can use HTML tags like <header>
, <nav>
, <main>
, <aside>
, and <footer>
to create a basic layout. For example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<meta name="viewport" content="width=device - width, initial - scale=1.0">
<title>My WordPress Theme</title>
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url');?>">
</head>
<body>
<header>
<h1><?php bloginfo('name');?></h1>
<p><?php bloginfo('description');?></p>
</header>
<nav>
<!-- Navigation menu goes here -->
</nav>
<main>
<!-- Main content goes here -->
</main>
<aside>
<!-- Sidebar content goes here -->
</aside>
<footer>
<p>© <?php echo date('Y');?> <?php bloginfo('name');?></p>
</footer>
</body>
</html>
You can use CSS to style the HTML elements. For example, to style the header:
header {
background - color: #333;
color: white;
text - align: center;
padding: 20px;
}
WordPress template tags are PHP functions that allow you to display specific content on your theme. Here are some common template tags:
the_title()
: Displays the title of the current post or page.the_content()
: Displays the content of the current post or page.wp_nav_menu()
: Displays a navigation menu.Example of using wp_nav_menu()
:
<nav>
<?php
wp_nav_menu( array(
'theme_location' => 'primary',
'menu_class' => 'nav - menu'
) );
?>
</nav>
The WordPress loop is used to display a list of posts. Here is a basic example:
<?php
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
?>
<article>
<h2><a href="<?php the_permalink();?>"><?php the_title();?></a></h2>
<p><?php the_excerpt();?></p>
</article>
<?php
endwhile;
endif;
?>
Hooks and filters are used to modify the behavior of WordPress without directly editing the core files. There are two types of hooks: action hooks and filter hooks.
wp_head
action hook is used to add custom code to the <head>
section of the HTML document.function my_custom_head_code() {
echo '<meta name="keywords" content="WordPress, custom theme">';
}
add_action( 'wp_head', 'my_custom_head_code' );
the_content
filter to add custom text after each post content.function add_custom_text_to_content( $content ) {
return $content. '<p>Thank you for reading!</p>';
}
add_filter( 'the_content', 'add_custom_text_to_content' );
It is important to manage your theme’s assets, such as CSS, JavaScript, and images. You can use the wp_enqueue_style
and wp_enqueue_script
functions to load these assets properly.
function my_custom_theme_assets() {
wp_enqueue_style( 'my - custom - style', get_stylesheet_uri() );
wp_enqueue_script( 'my - custom - script', get_template_directory_uri(). '/js/custom.js', array(), '1.0', true );
}
add_action( 'wp_enqueue_scripts', 'my_custom_theme_assets' );
Make sure your theme is responsive, which means it looks and functions well on different devices, such as desktops, tablets, and mobile phones. You can use media queries in CSS to achieve this. For example:
@media (max - width: 768px) {
body {
font - size: 14px;
}
}
Creating a WordPress theme using HTML, CSS, and PHP allows you to have full control over the design and functionality of your website. By understanding the fundamental concepts, following common practices, and implementing best practices, you can create a high - quality, professional - looking WordPress theme. Remember to test your theme thoroughly before deploying it to a live website.