Boost your WordPress development with this organized reference guide. Quickly find and implement common functions for The Loop, theme development, conditional logic, and more.
The core of WordPress for displaying post content. These are used inside the main post loop.
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<p>Post ID: <?php the_ID(); ?></p>
<?php $current_post_id = get_the_ID(); ?>
<h2><?php the_title(); ?></h2>
<?php $post_title = get_the_title(); ?>
<div class="entry-content"><?php the_content(); ?></div>
<div class="entry-summary"><?php the_excerpt(); ?></div>
<a href="<?php the_permalink(); ?>">Read More</a>
<p>By <?php the_author(); ?></p>
<div class="post-author"><?php the_author_posts_link(); ?></div>
<span class="post-date"><?php the_date('F j, Y'); ?></span>
<div class="post-categories"><?php the_category(', '); ?></div>
<div class="post-tags"><?php the_tags('Tags: ', ', ', ''); ?></div>
<?php rewind_posts(); ?>
Functions for building and managing theme structure and assets.
<?php get_header(); ?>
<?php get_footer(); ?>
<?php get_sidebar('primary'); // Includes sidebar-primary.php if it exists ?>
<?php get_template_part('template-parts/content', get_post_type()); ?>
<?php get_search_form(); ?>
<?php wp_head(); ?>
<?php wp_footer(); ?>
<body <?php body_class(); ?>>
function my_theme_styles() { wp_enqueue_style( 'my-custom-style', get_template_directory_uri() . '/css/custom.css' ); } add_action( 'wp_enqueue_scripts', 'my_theme_styles' );
function my_theme_scripts() { wp_enqueue_script( 'my-custom-script', get_template_directory_uri() . '/js/custom.js', array('jquery'), null, true ); } add_action( 'wp_enqueue_scripts', 'my_theme_scripts' );
<img src="<?php echo get_stylesheet_directory_uri(); ?>/images/logo.png">
<script src="<?php echo get_template_directory_uri(); ?>/js/parent-theme.js"></script>
function theme_setup() { add_theme_support( 'post-thumbnails' ); } add_action( 'after_setup_theme', 'theme_setup' );
function register_my_menus() { register_nav_menus( array( 'primary' => __( 'Primary Menu' ), 'footer' => __( 'Footer Menu' ) ) ); } add_action( 'init', 'register_my_menus' );
<?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?>
function my_register_sidebars() { register_sidebar(array('id' => 'primary', 'name' => 'Primary Sidebar')); } add_action( 'widgets_init', 'my_register_sidebars' );
<?php if ( is_active_sidebar( 'primary' ) ) : dynamic_sidebar( 'primary' ); endif; ?>
Functions for retrieving and managing data about posts, pages, and custom post types.
<?php $my_post = get_post(123); echo $my_post->post_title; ?>
<?php $args = array( 'posts_per_page' => 5 ); $latest_posts = get_posts( $args ); ?>
<?php $new_post = array('post_title' => 'My New Post', 'post_content' => 'This is the content.', 'post_status' => 'publish'); $post_id = wp_insert_post($new_post); ?>
<?php if ( has_post_thumbnail() ) { // Display the thumbnail } ?>
<?php the_post_thumbnail('thumbnail'); // Options: thumbnail, medium, large, full ?>
<?php $image_url = get_the_post_thumbnail_url(get_the_ID(), 'full'); ?>
<?php $custom_field = get_post_meta(get_the_ID(), 'my_custom_field_key', true); ?>
<?php update_post_meta(123, 'my_custom_field_key', 'New Value'); ?>
<?php echo get_permalink(123); ?>
<div class="nav-next"><?php next_post_link( '%link', 'Next Post »' ); ?></div>
<div class="nav-previous"><?php previous_post_link( '%link', '« Previous Post' ); ?></div>
Functions that check for a specific condition and return TRUE or FALSE. Often used with `if` statements.
<?php if ( is_front_page() ) { /* Front page specific code */ } ?>
<?php if ( is_home() && ! is_front_page() ) { /* Blog page specific code */ } ?>
<?php if ( is_single() ) { /* Single post specific code */ } ?>
<?php if ( is_page('contact-us') ) { /* Contact page specific code */ } ?>
<?php if ( is_category('news') ) { /* News category specific code */ } ?>
<?php if ( is_tag() ) { /* Any tag archive page */ } ?>
<?php if ( is_author(4) ) { /* Code for author with ID 4 */ } ?>
<?php if ( is_archive() ) { /* General archive page code */ } ?>
<?php if ( is_search() ) { /* Search results page code */ } ?>
<?php if ( is_404() ) { /* 404 page specific code */ } ?>
<?php if ( ! is_admin() ) { /* Code that runs only on the front-end */ } ?>
<?php if ( has_tag('featured') ) { /* Featured post code */ } ?>
<?php if ( has_category('tutorials') ) { /* Tutorial post code */ } ?>
<?php if ( is_active_sidebar( 'primary' ) ) { /* Code to display sidebar container */ } ?>
Functions for managing and retrieving user data.
<?php if ( is_user_logged_in() ) { echo 'Welcome back!'; } else { echo 'Welcome, guest!'; } ?>
<?php $current_user = wp_get_current_user(); echo 'Username: ' . $current_user->user_login; ?>
<?php $user_id = get_current_user_id(); ?>
<?php if ( current_user_can('edit_posts') ) { echo 'You can edit posts.'; } ?>
<?php echo get_avatar( get_the_author_meta('ID'), 64 ); // 64 is the size in pixels ?>
<?php echo 'Author bio: ' . get_the_author_meta('description'); ?>
Functions for generating and retrieving URLs within WordPress.
<a href="<?php echo esc_url( home_url( '/' ) ); ?>">Go Home</a>
<?php echo site_url(); ?>
<a href="<?php echo esc_url( admin_url( 'profile.php' ) ); ?>">Edit Profile</a>
<?php echo includes_url(); ?>
<?php echo content_url(); ?>
<img src="<?php echo plugins_url( 'my-plugin/images/icon.png' ); ?>">
Functions for interacting directly with the WordPress database and the options table.
<?php $blog_description = get_option( 'blogdescription' ); ?>
<?php update_option( 'my_custom_setting', 'new_value' ); ?>
<h1><?php bloginfo('name'); ?></h1>
global $wpdb; $results = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}posts WHERE post_status = 'publish'");
Functions that allow you to "hook" your custom code into WordPress execution at specific points.
// In functions.php: function my_custom_function() { /* do something */ } add_action('wp_head', 'my_custom_function');
// In functions.php: function change_excerpt_length($length) { return 20; } // 20 words add_filter('excerpt_length', 'change_excerpt_length');
<?php do_action('my_custom_action_before_footer'); ?>
// In a plugin or theme: $title = apply_filters('my_custom_title', 'Default Title');