Beginner's Guide to WP Code Placement
| File | What Goes Here | When to Edit |
|---|---|---|
functions.php | Add/remove features, register menus, enqueue scripts/styles, custom hooks | Most customizations go here |
style.css | Theme styles + required theme header comment | Styling changes |
index.php | Fallback template for all content | Rarely — use specific templates instead |
page.php | Template for static Pages | Custom page layouts |
single.php | Template for single blog Posts | Custom post detail layout |
header.php | Site <head> and opening <header> HTML | Editing site-wide top section |
footer.php | Closing HTML, scripts, footer widget area | Editing site-wide bottom section |
sidebar.php | Sidebar widget area | Custom sidebar layout |
Template Tags
| Snippet | Code |
|---|---|
| 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
| Snippet | Code |
|---|---|
| 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
| Snippet | Code |
|---|---|
| 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
| Snippet | Code |
|---|---|
| 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
| Snippet | Code |
|---|---|
| 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
|