2. Digging Deeper Into functions.php
With functions.php you can:
• Use WordPress hooks. For example, with the
excerpt_length filter you can change your post excerpt
length (from default of 55 words).
• Enable WordPress features with
add_theme_support(). For example, turn on post
thumbnails, post formats, and navigation menus.
• Define functions you wish to reuse in multiple theme
template files.
3. Theme Setup
A number of theme features should be included within a
“setup” function that runs initially when your theme is
activated.You can do this in functions.php.
if ( ! function_exists( 'myfirsttheme_setup' ) ) :
/**
* Sets up theme defaults and registers support for various WordPress features
*
*
* @since MyFirstTheme 1.0
*/
function myfirsttheme_setup() {
4. Theme Setup
A number of theme features should be included within a
“setup” function that runs initially when your theme is
activated.You can do this in functions.php.
if ( ! function_exists( 'myfirsttheme_setup' ) ) :
/**
* Sets up theme defaults and registers support for various WordPress features
*
*
* @since MyFirstTheme 1.0
*/
function myfirsttheme_setup() {
Reminder: Make SureYour Namespaces Are Unique
5. Theme Setup
Here are four examples of what you can add to your
theme setup function.
• Automatic Feed Links
• Custom Logo
• Initialize Menus
• Post Thumbnails
6. Theme Setup
Automatic Feed Links
add_theme_support( 'automatic-feed-links' );
Automatic feed links enables post and comment RSS
feeds by default.These feeds will be displayed in <head>
automatically.They can be called using
add_theme_support().
7. Theme Setup
Custom Logo
add_theme_support( 'custom-logo', array(
'height' => 100,
'width' => 400,
'flex-height' => true,
'flex-width' => true,
'header-text' => array( 'site-title', 'site-description' ),
) );
Theme Logo is a theme feature, first introduced in
WordPressVersion 4.5.This feature allows themes to add
custom logos.
https://codex.wordpress.org/Theme_Logo
8. Theme Setup
Navigation Menus
register_nav_menus( array(
'primary' => __( 'Primary Menu', 'myfirsttheme' ),
'secondary' => __( 'Secondary Menu', 'myfirsttheme' )
) );
You can set up multiple menus in functions.php.They can
be added using register_nav_menus() and inserted into a
theme using wp_nav_menu(),
9. Theme Setup
Post Thumbnails
add_theme_support( 'post-thumbnails' );
Post thumbnails and featured images allow your users to
choose an image to represent their post.Your theme can
decide how to display them, depending on its design.
While not every theme needs featured images, it’s recommended that you
support post thumbnails and featured images.
11. Registering Additional Image
Sizes for your Theme
You will need to start by adding the support of post
thumbnails by placing the following code in your
theme’s functions.php file:
add_theme_support( 'post-thumbnails' );
12. Registering Additional Image
Sizes for your Theme
Once you enable the support for post thumbnails, you
can now use the functionality of registering additional
image sizes with the function add_image_size().
add_image_size( 'sidebar-thumb', 120, 120, true ); // Hard Crop Mode
add_image_size( 'homepage-thumb', 220, 180 ); // Soft Crop Mode
add_image_size( 'singlepost-thumb', 590, 9999 ); // Unlimited Height Mode
13. Registering Additional Image
Sizes for your Theme
Finally, open the theme file where you want to display
the image and paste the following code:
<?php the_post_thumbnail( 'your-specified-image-size' ); ?>
This bit of code must be pasted inside the post loop.
14. Registering Additional Image
Sizes for your Theme
Finally, open the theme file where you want to display
the image and paste the following code:
<?php the_post_thumbnail( 'your-specified-image-size' ); ?>
This bit of code must be pasted inside the post loop.