Beginner's Guide to WP Code Placement
FileWhat Goes HereWhen to Edit
functions.phpAdd/remove features, register menus, enqueue scripts/styles, custom hooksMost customizations go here
style.cssTheme styles + required theme header commentStyling changes
index.phpFallback template for all contentRarely — use specific templates instead
page.phpTemplate for static PagesCustom page layouts
single.phpTemplate for single blog PostsCustom post detail layout
header.phpSite <head> and opening <header> HTMLEditing site-wide top section
footer.phpClosing HTML, scripts, footer widget areaEditing site-wide bottom section
sidebar.phpSidebar widget areaCustom sidebar layout
Template Tags
SnippetCode
Site URL Outputs the home URL of the site
echo home_url(); // https://yourdomain.com
Site Title Outputs blog name from Settings
echo get_bloginfo('name');
The Loop Core loop — always wraps post output
if ( have_posts() ) : while ( have_posts() ) : the_post(); the_title(); // output title the_content(); // output content endwhile; endif;
Post Title Echo current post title inside the loop
the_title(); // inside The Loop // Outside the loop: echo get_the_title( $post_id );
Featured Image Display the post thumbnail
the_post_thumbnail( 'medium' ); // Sizes: thumbnail, medium, large, full
Permalink Get URL of current post
echo get_permalink(); // Or output a link: the_permalink();
Functions.php Essentials
SnippetCode
Enqueue Scripts & Styles Proper way to add CSS/JS to your theme — never hardcode in header.php
add_action( 'wp_enqueue_scripts', 'my_theme_scripts' ); function my_theme_scripts() { wp_enqueue_style( 'main-style', get_stylesheet_uri() ); wp_enqueue_script( 'main-js', get_template_directory_uri() . '/js/main.js', [], '1.0.0', true // true = load in footer ); }
Register Navigation Menu Registers a menu location — assign menu in Appearance → Menus
add_action( 'after_setup_theme', 'register_my_menus' ); function register_my_menus() { register_nav_menus([ 'primary' => 'Primary Navigation', 'footer-nav' => 'Footer Navigation', ]); }
Add Theme Support Enable core WP features for your theme
add_action( 'after_setup_theme', 'my_theme_setup' ); function my_theme_setup() { add_theme_support( 'post-thumbnails' ); add_theme_support( 'title-tag' ); add_theme_support( 'html5', [ 'comment-list', 'comment-form', 'search-form', 'gallery' ]); add_theme_support( 'woocommerce' ); // if using WC }
Register Sidebar/Widget Area Creates a widget area you can fill in Appearance → Widgets
add_action( 'widgets_init', 'my_register_sidebars' ); function my_register_sidebars() { register_sidebar([ 'name' => 'Main Sidebar', 'id' => 'main-sidebar', 'before_widget' => '<div class="widget">', 'after_widget' => '</div>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>', ]); }
WP_Query — Custom Post Queries
SnippetCode
Basic Custom Query Fetch posts with specific criteria — always reset after use
$args = [ 'post_type' => 'post', 'posts_per_page' => 6, 'orderby' => 'date', 'order' => 'DESC', ]; $query = new WP_Query( $args ); if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); the_title(); endwhile; wp_reset_postdata(); // ALWAYS reset endif;
Query by Category Get posts from a specific category by slug
$args = [ 'post_type' => 'post', 'posts_per_page' => 4, 'tax_query' => [[ 'taxonomy' => 'category', 'field' => 'slug', 'terms' => 'news', ]], ];
Query Custom Post Type Fetch a custom post type (must be registered first)
$args = [ 'post_type' => 'portfolio', // your CPT slug 'posts_per_page' => -1, // -1 = all 'meta_key' => 'featured', 'meta_value' => 'yes', ];
Hooks: Actions & Filters
SnippetCode
Add Action — Custom Hook Run your function at a specific WP event
// Syntax: add_action( hook, function, priority, args ) add_action( 'wp_head', 'add_custom_meta' ); function add_custom_meta() { echo '<meta name="author" content="Zach">'; }
Add Filter — Modify Output Intercept and change existing data before output
// Modify the excerpt length add_filter( 'excerpt_length', 'custom_excerpt_length' ); function custom_excerpt_length( $length ) { return 20; // 20 words }
Remove Default Action Unhook something WordPress or a plugin does by default
// Remove WP version from <head> remove_action( 'wp_head', 'wp_generator' ); // Remove admin bar on frontend add_filter( 'show_admin_bar', '__return_false' );
User & Conditional Tags
SnippetCode
Check if User is Logged In Show/hide content based on login state
if ( is_user_logged_in() ) { echo 'Welcome, member!'; } else { echo '<a href="' . wp_login_url() . '">Log In</a>'; }
Conditional Template Tags Target specific page types in templates
is_home() // Blog index is_front_page() // Static front page is_page('about') // Specific page by slug is_single() // Single post is_category() // Category archive is_archive() // Any archive page is_search() // Search results is_404() // 404 error page
Get Current User Info Access logged-in user data
$user = wp_get_current_user(); echo $user->display_name; // Full name echo $user->user_email; // Email echo $user->user_login; // Username