0% found this document useful (0 votes)
26 views196 pages

Unit 4

The document discusses theme development in WordPress. It covers the WordPress loop, template tags like the_post(), have_posts(), get_header(), get_footer(), get_sidebar() and how they work. It also discusses hooks in WordPress and functions like wp_title(), single_post_title().

Uploaded by

Reshma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views196 pages

Unit 4

The document discusses theme development in WordPress. It covers the WordPress loop, template tags like the_post(), have_posts(), get_header(), get_footer(), get_sidebar() and how they work. It also discusses hooks in WordPress and functions like wp_title(), single_post_title().

Uploaded by

Reshma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
You are on page 1/ 196

Theme development

Prepared by Sejal Thumar


Theme development
• The Loop
• Template Tags
– 1. General tags
– 2. Author tags
– 3. Category tags
– 4. Link tags
– 5. Post tags
– 6. Post Thumbnail tags
– 7. Navigation Menu tags
– 8. Conditional Tags
The Loop Overview
• The loop, or WordPress loop or simply loop, is PHP code that
displays WordPress posts.

• The loop is used in WordPress themes to display a list of posts in a


web page. Inside the loop there are some functions that are run
by default to display posts.

• There are several Template tags that work only inside the
WordPress loop and can be used to format, arrange, and
publish post data.
What is the Wordpress Loop?
So how does it work?
What does a loop display?
What does a loop display?
EXAMPLE
<?php if (have_posts()) : while ( have_posts() ) :
the_post(); ?>

<h2 id="post-<?php the_ID(); ?>">


<a href="<?php the_permalink() ?>" rel="bookmark"
title="Permanent Link to <?php the_title(); ?>">
<?php the_title(); ?></a>
</h2>

<p class="date-author">Posted: <?php the_date(); ?> by


<?php the_author(); ?></p> <?php the_content(); ?>
EXAMPLE

<p class="postmetadata">
Filed in: <?php the_category(); ?> | Tagged: <?
php the_tags(); ?> | <a href="<?php comments_link(); ?
>" title="Leave a
comment">Comments</a>
</p>
<?php endwhile;
else : ?>
<p>Sorry no posts matched your
criteria.</p>
<?php endif; ?>
have_posts()
• This function checks to see if the current WordPress
query has any results to loop over.

• This is a boolean function, meaning it returns either


TRUE or FALSE.

• As a side effect, have_posts starts, steps through,


or resets The Loop.

• At the end of the loop, have_posts returns 0 after


calling rewind_posts.
have_posts()

• Usage
<?php have_posts(); ?>
• Parameters
This function does not accept any parameters.

• Return Values
(boolean) True on success, false on failure.
have_posts()
• The loop starts here:

<?php if ( have_posts() ) : while ( have_posts() ) :


the_post(); ?>

• and ends here:


<?php endwhile; else : ?>
<p> Sorry, no posts matched your criteria
</p>
<?php endif; ?>
have_posts()

• This is using PHP's alternative syntax for control


structures, and could also be expressed as:
the_post
• The 'the_post' action hook allows developers to
modify the post object immediately after being
queried and setup.

• The post object is passed to this hook by reference


so there is no need to return a value

• WP_Query::the_post() sets a Boolean flag to notify


other class members that we're in the Loop, Then it
checks whether The Loop has started and sets the
current post by moving each time to the next post in
the queue, until the post queue ends.
the_post Example-1

function my_the_post_action( $post_object )


{
//modify post object here
}
add_action( 'the_post', 'my_the_post_action' );
the_post Example -2
if ( have_posts() )
{
while ( have_posts() )
{ the_post(); ?>

<h2> <?php the_title(); ?> </h2>


<?php the_content();
}
}
?>
What is meant by hooks in Wordpress?

• Hooks are provided by WordPress to allow your


plugin to 'hook into' the rest of WordPress; that is, to
call functions in your plugin at specific times, and
thereby set your plugin in motion.

• There are two kinds of hooks:


– Actions (Codex Action Reference)
– Filters (Codex Filter Reference)
What is meant by hooks in Wordpress?

• For example, if you want your plugin to change the


text of a post, you might add an action function to
publish_post (so the post is modified as it is saved
to the database), or a filter function to the_content
(so the post is modified as it is displayed in the
browser screen).
What is meant by hooks in Wordpress?
What is meant by hooks in Wordpress?
Wp_head()
• Fire the 'wp_head' action. Put this template tag
anywhere inside <head> tag in a theme template
(ex. header.php, index.php).

– Always have wp_head() just before the closing </head>


tag of your theme.

– or you will break many plugins, if you not use wp_head().

– which generally use this hook to add elements to <head>


such as styles, scripts, and meta tags.
Wp_head()
• wp_head() is located in wp-includes/general-template.php.

• This function does not accept any parameters.

• Return values is None.

• Three hooks should be made available in every theme to ensure


compatibility with the most number of Wordpress users:
– Wp_head()
– Wp_footer()
– Comment_form()
get_footer()
• Includes the footer.php template file from your current theme's
directory.

• if a name is specified then a specialised footer


footer{name}.php will be included.

• If the theme contains no footer.php file then the footer from the
default theme wp-includes/theme-compat/footer.php will be
included.
get_footer()
Syntax:
<?php get_footer( $name ); ?>
Parameters:
• $name (string) (optional) Calls for footer-name.php.

Example :
index.php
<?php get_header(); ?>
<h2>Error 404 - Not Found</h2>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
get_header()
• Includes the header.php template file from your current
theme's directory.

• If a name is specified then a specialised header header-


{name}.php will be included.

• If the theme contains no header.php file then the header from


the default theme wp-includes/theme-compat/header.php will
be included.

Usage
• <?php get_header( $name ); ?>
get_header()
Parameters
• $name (string) (optional) Calls for header-name.php.
• Default: None

Multiple Headers
• Different header for different pages.
<?php
if ( is_home() ) : get_header( 'home' );
elseif ( is_404() ) : get_header( '404' );
else : get_header();
endif; ?>
get_sidebar( )
• Load sidebar template.

• Includes the sidebar template for a theme or if a name is


specified then a specialised sidebar will be included.

• For the parameter, if the file is called "sidebar-special.php"


then specify "special".

• Parameters
• $name (string) (Optional) The name of the specialised
sidebar.
• Default value: null
get_sidebar( )
• Simple call
Assume you have file wp-content/yourTheme/sidebar-nice-
bar.php.

• The way you can include this sidebar in your page is:
<?php get_sidebar('nice-bar'); ?>

• Left and Right Sidebars Two sidebars in one theme.


<?php get_header(); ?>
<?php get_sidebar( 'left' ); ?>
<?php get_sidebar( 'right' ); ?>
<?php get_footer(); ?>
get_search_form( )
• Display search form.

• Will first attempt to locate the searchform.php file in either the


child or the parent, then load it.

• If it doesn’t exist, then the default search form will be displayed.

• The default search form is HTML, which will be displayed.

• There is a filter applied to the search form HTML in order to edit


or replace it. The filter is ‘get_search_form’.
get_search_form( )
• This function is primarily used by themes which want to
hardcode the search form into the sidebar and also by the
search widget in WordPress.

• There is also an action that is called whenever the function


is run called, ‘pre_get_search_form’.

• This can be useful for outputting JavaScript that the search


relies on or various formatting that applies to the beginning
of the search.

• To give a few examples of what it can be used for.


get_search_form( )
Parameters
• $echo (bool) (Optional) Default to echo and not return the form.
• Default value: true

Return
• (string|void) String when $echo is false.

function wpdocs_after_setup_theme()
{
add_theme_support( 'html5', array( 'search-form' ) );
}
add_action( 'after_setup_theme', 'wpdocs_after_setup_theme' );
wp_title( )
• Display or retrieve page title for all areas of blog.

• By default, the page title will display the separator before the
page title, so that the blog title will be before the page title.

• This is not good for title display, since the blog title shows up on
most tabs and not what is important, which is the page that the
user is looking at.

• There are also SEO benefits to having the blog title after or to
the ‘right’ of the page title.
wp_title( )
• However, it is mostly common sense to have the blog title to the
right with most browsers supporting tabs.

• You can achieve this by using the seplocation parameter and


setting the value to ‘right’.

• This change was introduced around 2.5.0, in case backward


compatibility of themes is important.

Parameters
• $sep
(string) (Optional) default is '»'. How to separate the various
items within the page title. Default value: '&raquo;'
wp_title( )
• $display
(bool) (Optional) Whether to display or retrieve title.
Default value: true

• $seplocation
(string) (Optional) Direction to display title, 'right'.
Default value: '‘

• Return
(string | null) String on retrieve, null when displaying.
wp_title( )
• Return Values
• The function returns a concatenated string. It always queries
the database for a default string; the value of the default string
depends on the type of post or page:

Single post
• the title of the post
Date-based archive
• the date (e.g., “2006”, “2006 – January”)
Category
• the name of the category
Author page
• the public name of the user
wp_title( )
<html>
<head>
<title><?php wp_title(); ?></title>
<?php wp_head(); ?>
</head>
single_post_title();
Description
• Displays or returns the title of the post when on a
single post page (permalink page).

• This tag can be useful for displaying post titles outside


The Loop.

• single_post_title() is located in
wp-includes/general-template.php.
Default Usage
• <?php single_post_title(); ?>
single_post_title();
Usage
<?php single_post_title( $prefix, $display ); ?>

Parameters
• $prefix (string) (optional) Text to place before the title.
Default: None

• $display (boolean) (optional) Should the title be


displayed (TRUE) or returned for use in PHP (FALSE).
Default: TRUE
comments_template()
• WordPress displays comments in your theme based
on the settings and code in the comments.php file
within your WordPress theme.

• The comments.php template contains all the logic


needed to pull comments out of the database and
display them in your theme.

• Load the comment template specified in $file.


comments_template()
comments_template( string $file = '/comments.php',
bool $separate_comments = false )

Description
• Will not display the comments template if not on single post or
page, or if the post does not have comments.

• Uses the WordPress database object to query for the


comments. The comments are passed through the
‘comments_array’ filter hook with the list of comments and the
post ID respectively.
comments_template()
• The $file path is passed through a filter hook called
‘comments_template’, which includes the TEMPLATEPATH
and $file combined.

• Tries the $filtered path first and if it fails it will require the
default comment template from the default theme.

• If either does not exist, then the WordPress process will be


halted. It is advised for that reason, that the default theme is
not deleted.
comments_template()
Parameters
$file
(string) (Optional) The file to load.
Default value: '/comments.php'

$separate_comments
(bool) (Optional) Whether to separate the comments by
comment type.
Default value: false
comments_template()
• On some occasions you may want display your comments
differently within your theme.

• For this you would build an alternate file (ex. short-


comments.php) and call it as follows:

<?php comments_template( '/short-comments.php' ); ?


>
comments_template()
Single.php
Add comment on Custom post type
Add comment on Custom post type
add_theme_support( )
• Registers theme support for a given feature.

Description
• Must be called in the theme’s functions.php file to work. If
attached to a hook, it must be ‘after_setup_theme’. The ‘init’
hook may be too late for some features.
add_theme_support( )

Parameters
$feature
(string) (Required)
The feature being added. Likely core values include
'post-formats', 'post-thumbnails', 'html5', 'custom-logo', 'custom-
header-uploads', 'custom-header', 'custom-background', 'title-
tag', 'starter-content', etc.

$args,...
(mixed) (Optional)
extra arguments to pass along with certain features.
add_theme_support( )

Return
• (void|bool) False on failure, void otherwise.

Post Formats
• This feature enables Post Formats support for a theme. When
using child themes, be aware that

• add_theme_support( 'post-formats' )

• will override the formats as defined by the parent theme, not


add to it.
add_theme_support( )
• add_theme_support( 'post-formats', array( 'aside', 'gallery' ) );

• To check if there is a ‘quote’ post format assigned to the post,


use has_post_format():

• // In your theme single.php, page.php or custom post type


if ( has_post_format( 'quote' ) )
{
echo 'This is a quote.';
}
body_class()
• Display the classes for the body element.

Parameters
• $class (string|array) (Optional) One or more classes to add to
the class list.
• Default value: '‘

Example
<body <?php body_class(); ?>>
• <body class="page page-id-2 page-parent page-template-
default logged-in">
body_class()

Adding More Classes

• By default, the only classes will be those described above.

• To add more classes, the template tag’s parameter can be


added. For example, to add a unique class to the same
template used above:

• <body <?php body_class( 'class-name' ); ?>>


add_filter

Hook a function or method to a specific filter action.


Description
• WordPress offers filter hooks to allow plugins to modify various
types of internal data at runtime.

• A plugin can modify data by binding a callback to a filter hook.


When the filter is later applied, each bound callback is run in
order of priority, and given the opportunity to modify a value by
returning a new value.

• The following example shows how a callback function is bound


to a filter hook.
add_filter
• Note that $example is passed to the callback, (maybe)
modified, then returned:

• Note that the filter function must return the array of classes after
it is finished processing, or all of the classes will be cleared and
could seriously impact the visual state of a user's site.

• filter_function_name should be unique function name.

• It cannot match any other function name already declared.


add_filter

// Apply filter add_filter


function example_callback( $example )
{
// Maybe modify $example in some way.
return $example;
}
add_filter( 'example_filter', 'example_callback' );
add_filter

Parameters
• $tag(string) (Required)
• The name of the filter to hook the $function_to_add callback to.

• $function_to_add(callable) (Required)
• The callback to be run when the filter is applied.

• $priority(int) (Optional)
• Used to specify the order in which the functions associated with
a particular action are executed.
• Lower numbers correspond with earlier execution, and
functions with the same priority are executed in the order in
which they were added to the action. Default value: 10
add_filter

Parameters
• $accepted_args(int) (Optional)
• The number of arguments the function accepts.
• Default value: 1
add_filter

wp-includes/plugin.php
function add_filter( $tag, $function_to_add, $priority = 10,
$accepted_args = 1 )
{
global $wp_filter;
if ( ! isset( $wp_filter[ $tag ] ) )
{
$wp_filter[ $tag ] = new WP_Hook();
}
$wp_filter[ $tag ]->add_filter( $tag, $function_to_add, $priority,
$accepted_args );
return true;
}
add_action()
• Hooks a function on to a specific action.

Description
• Actions are the hooks that the WordPress core launches at
specific points during execution, or when specific events occur.

• Plugins can specify that one or more of its PHP functions are
executed at these points, using the Action API.
add_action()

Parameters
• $tag
(string) (Required)
The name of the action to which the $function_to_add is
hooked.

• $function_to_add
(callable) (Required)
The name of the function you wish to be called.
add_action()

Parameters
• $tag
(string) (Required)
The name of the action to which the $function_to_add is
hooked.

• $function_to_add
(callable) (Required)
The name of the function you wish to be called.
add_action()
• $priority
(int) (Optional)
Used to specify the order in which the functions associated with
a particular action are executed.

Lower numbers correspond with earlier execution, and


functions with the same priority are executed in the order in
which they were added to the action.
• Default value: 10
add_action()
• Return
• (true) Will always return true.
2. Author tags
1. the_author()
Description
• The author of a post can be displayed by using this
Template Tag. This tag must be used within The Loop.

• To return to PHP rather than displaying, use


get_the_author().

Usage
• <?php the_author(); ?>
1. the_author()
Examples
• Display Author's 'Public' Name
• Displays the value in the user's Display name publicly
as field.

<p>This post was written by <?php the_author(); ?></p>

• Parameters
• $post-id (integer).
2. get_the_author()
Description
• Retrieve the post author's display name. This tag must
be used within The Loop.

• To get the post author's ID, use get_the_author_meta(


'ID' ).

• To display a page for authors which have no posts,


see this discussion.
2. get_the_author()
Usage
• <?php $author = get_the_author(); ?>

Parameters
• $deprecated (string) (optional) Deprecated.
Default: '‘

Returns
• (string) The author's display name.
2. get_the_author()
Examples
• Grab the Author's 'Public' Name
• Grabs the value in the user's Display name publicly as
field.
• <?php $author = get_the_author(); ?>

Source File
• get_the_author() is located in
wp-includes/author-template.php.
3. the_author_link()
Description
• This tag displays a link to the Website for the author of
a post.

• The Website field is set in the user's profile (


Administration > Profile > Your Profile).

• The text for the link is the author's Profile Display


name publicly as field. This tag must be used within
The Loop.
3. the_author_link()
Usage
• <?php the_author_link(); ?>

Parameters
• This function does not accept any parameters.

Example
• Displays the author's Website URL as a link and the text
for the link is the author's Profile Display name publicly
as field. In this example, the author's Display Name is
James Smith.
• <p>Written by: <?php the_author_link(); ?></p>
4. get_the_author_link()
Description
• This tag returns a link to the Website for the author of
a post.

• The Website field is set in the user's profile (


Administration > Users > Your Profile).

• The text for the link is the author's Profile Display


name publicly as field. This tag must be used within
The Loop.
4. get_the_author_link()

• get_the_author_link() returns the link for use in PHP.


To display the link instead, use the_author_link().

Usage
• <?php get_the_author_link(); ?>

Parameters
• This tag does not accept any parameters.
4. get_the_author_link()
Example
• The example echos (displays) the author's Website
URL as a link and the text for the link is the author's
Profile Display name publicly as field. In this example,
the author's Display Name is James Smith.

<p>Written by: <?php echo get_the_author_link(); ?


></p>

Source File
• get_the_author_link() is located in
wp-includes/author-template.php
6. the_author_meta()
• Description
• The the_author_meta Template Tag displays a
desired meta data field for a user. Only one field is
returned at a time, you need to specify which you
want.
• If this tag is used within The Loop, the user ID value
need not be specified, and the displayed data is that of
the current post author. A user ID can be specified if
this tag is used outside The Loop.
6. the_author_meta()
• If the meta field does not exist, nothing is printed.
• NOTE: Use get_the_author_meta() if you need to
return (and do something with) the field, rather than
just display it.

Usage
• <?php the_author_meta( $field, $userID ); ?>

Parameters
• $field (string) Field name for the data item to be
displayed. Valid values:
6. the_author_meta()
• user_login • last_name
• user_pass • description
• user_nicename • jabber
• user_email • aim
• user_url • yim
• • user_level
user_registered
• user_firstname
• user_activation_key • user_lastname
• user_status • user_description
• display_name • rich_editing
• nickname • comment_shortcuts
• first_name • admin_color
6. the_author_meta()
$user ID
(integer) (optional) If the user ID fields is used, then this
function display the specific field for this user ID.

Default: false

Examples
• Display the Author's AIM screenname
• Displays the value in the author's AIM (AOL Instant Messenger
screenname) field.
• <p>This author's AIM address is <?php
the_author_meta('aim'); ?></p>
6. the_author_meta()
Display a User Email Address
• Displays the email address for user ID 25.

<p>The email address for user id 25 is <?php


the_author_meta('user_email',25); ?></p>

• Advanced Uses
• A plugin may add an additional field in the registration or
manage users, which adds a new value in the wp_usermeta
table (where wp_ is your data base prefix). For this example
we will use a Twitter ID. For a meta_key value of "twitter" and
meta_value of "WordPress" then
6. the_author_meta()
<p>This author's Twitter name is <?php
the_author_meta('twitter'); ?></p>

would return
• This author's Twitter name is WordPress

Source File
• the_author_meta() is located in
wp-includes/author-template.php.
Category tags
1. the_category
Description
• Displays a link to the category or categories a post belongs to.
This tag must be used within The Loop.

Usage
• <?php the_category( $separator, $parents, $post_id ); ?>
Parameters
$separator (string) (optional) Text or character to display
between each category link. By default, the links are placed in
an HTML unordered list. An empty string will result in the
default behavior.

Default: empty string


1. the_category
$parents (string) (optional) How to display links that reside in
child (sub) categories. Options are:
▪ 'multiple' - Display separate links to parent and child
categories, exhibiting "parent/child" relationship.
▪ 'single' - Display link to child category only, with link text
exhibiting "parent/child" relationship. Default: empty string

$post_id (int) (optional) Post ID to retrieve categories. The default


value false results in the category list of the current post.
Default: false
1. the_category
Examples
• Separated by Space
• List categories with a space as the separator. <?
php the_category( ' ' ); ?>

• Separated by Comma
• Displays links to categories, each category separated by a
comma (if more than one). <?php the_category( ', ' ); ?>
1. the_category
• Separated by Arrow
• Displays links to categories with an arrow (>) separating the
categories. Note: Take care when using this, since some
viewers may interpret a category following a > as a
subcategory of the one preceding it. <?
php the_category( '&gt; ' ); ?>

• Separated by a Bullet

• Displays links to categories with a bullet (•) separating the


categories. <?php the_category( '&bull;' ); ?>
1. the_category
• Source Code
• the_category() is located in
wp-includes/category-template.php

• Changelog
• Since 0.71
• 2.5 : Added the post_id parameter.
2. category_description()
Description
• Returns the description of a category defined in the category
settings screen for the current category (Posts > Categories).

• If used in the archive.php template, place this function within


the is_category() conditional statement. Otherwise, this
function will stop the processing of the page for monthly and
other archive pages.

Usage
• <?php echo category_description( $category_id ); ?>
2. category_description()
Parameters
• $category_id (integer) (optional) The ID of the category to
return a description.

• Default: Description of current query category.

Example
1. Default Usage
• Displays the description of a category, given its id, by echoing
the return value of the tag. If no category given and used on a
category page, it returns the description of the current
category.
• <div><?php echo category_description(3); ?></div>
2. category_description()

2. Using Category Slug


• Displays the description of a category, using a category slug.
<?php
echo category_description( get_category_by_slug('category-
slug')->term_id );
?>

3. With Category Title


<div><strong><?php single_cat_title('Currently browsing'); ?>
</strong>: <?php echo category_description(); ?></div>
3. single_cat_title()
• Display or retrieve page title for category archive.

Description
• Useful for category template files for displaying the category
page title.

• The prefix does not automatically place a space between the


prefix, so if there should be a space, the parameter value will
need to have it at the end.
3. single_cat_title()
Parameters
• $prefix
(string) (Optional) What to display before the title.
Default value: ''
• $display
(bool) (Optional) Whether to display or retrieve title.
Default value: true
Return
• (string|void) Title when retrieving.

Example
<?php single_cat_title(“showing post from:”,true)?>
4. Link tags
1. the_permalink(),
Description
• Displays the URL for the permalink to the post currently being
processed in The Loop.

• This tag must be within The Loop, and is generally used to


display the permalink for each post, when the posts are being
displayed.

• Since this template tag is limited to displaying the permalink


for the post that is being processed, you cannot use it to
display the permalink to an arbitrary post on your weblog.

• Refer to get_permalink() if you want to get the permalink for a


post, given its unique post id.
1. the_permalink(),
Usage
<?php the_permalink(); ?>
Parameters
• Before 4.4.0, this tag has no parameters. Since 4.4.0: Added
the `$post` parameter.

Examples
1. Display Post URL as Text
Displays the URL to the post, without creating a link:
This address for this post is: <?php the_permalink(); ?>
Output: http://localhost/bca3/wordpress/?p=167
1. the_permalink(),
2. As Link With Text
• You can use whatever text you like as the link text, in this
case, "permalink".

<a href="<?php the_permalink(); ?>">permalink</a>

3. Used as Link With Title Tag


• Creates a link for the permalink, with the post's title as the link
text. This is a common way to put the post's title.
1. the_permalink(),

<a href="<?php the_permalink(); ?>" title="<?php


the_title_attribute(); ?>">
<?php the_title(); ?>
</a>

Source File
• the_permalink() is located in wp-includes/link-template.php.
2. get_permalink()
Retrieves the full permalink for the current post or post ID and
page.

Parameters
• $post
(int|WP_Post) (Optional) Post ID or post object.
Default is the global $post.

• $leavename
(bool) (Optional) Whether to keep post name or page name.
Default value: false
2. get_permalink()
Return
• (string|false) The permalink URL or false if post does not exist.

Example 1

<?php echo get_permalink( $post->ID ); ?>

Output: http://localhost/bca3/wordpress/introduction/
3. home_url()
Description
• The home_url template tag retrieves the home URL for the
current site, optionally with the $path argument appended.

• The function determines the appropriate protocol, "https" if


is_ssl() and "http" otherwise. If the $scheme argument is "http"
or "https" the is_ssl() check is overridden.

Usage
<?php home_url( $path, $scheme ); ?>

Default Usage
• <?php echo esc_url( home_url( '/' ) ); ?
3. home_url()
Parameters
• $path
(string) (optional) Path relative to the home URL.
Default: None

• $scheme
(string) (optional) Scheme to use for the home URL. Currently,
only "http", "https" and "relative" are supported.
Default: null

Return
• (string)
Home URL with the optional $path argument appended.
3. home_url()
Example 1
$url = home_url();
echo esc_url( $url );

Output: http://localhost/bca3/wordpress

Example 2

(Note the lack of a trailing slash)


$url = home_url( '/' );
echo esc_url( $url );
Output: http://localhost/bca3/wordpress/
3. home_url()
Example 3
$url = home_url( '/', 'https' );
echo esc_url( $url );

Output: https://localhost/bca3/wordpress/

Example 4

$url = home_url( 'word', 'relative' );


echo esc_url( $url );

Output: /bca3/wordpress/word
4. get_home_url()
• Retrieves the URL for a given site where the front end is
accessible.

Description
• Returns the ‘home’ option with the appropriate protocol.

• The protocol will be ‘https’ if is_ssl() evaluates to true;


otherwise, it will be the same as the ‘home’ option.

• If $scheme is ‘http’ or ‘https’, is_ssl() is overridden.


4. get_home_url()
Parameters
• $blog_id
(int) (Optional) Site ID. Default null (current site).
Default value: null
• $path
(string) (Optional) Path relative to the home URL.
Default value: ‘ ‘
• $scheme
(string|null) (Optional) Scheme to give the home URL
context. Accepts 'http', 'https', 'relative', 'rest', or null.
Default value: null
4. get_home_url()
Return
• (string) Home URL link with optional path appended.

Example:
<?php echo get_home_url(); ?>

Output: http://localhost/bca3/wordpress

Will output: https://www.example.com With the domain and the


schema matching your settings.
5. site_url()
Description
• The site_url template tag retrieves the site url for the current
site (where the WordPress core files reside) with the
appropriate protocol, 'https' if is_ssl() and 'http' otherwise.

• If scheme is 'http' or 'https', is_ssl() is overridden.

• Use this to get the "WordPress address" as defined in general


settings. Use home_url() to get the "site address" as defined in
general settings.
5. site_url()
Usage
• <?php site_url( $path, $scheme ); ?>

Default Usage
• <?php echo site_url(); ?>

Parameters
• $path
(string) (optional) Path to be appended to the site url.
Default: None
5. site_url()
$scheme
(string) (optional) Context for the protocol for the url
returned.

Setting $scheme will override the default context.


Allowed values are 'http', 'https', 'login', 'login_post',
'admin', or 'relative'.

Default: null
Return
• (string) Site url link with optional path appended.
5. site_url()
Examples
• $url = site_url(); echo $url;

• (Note the lack of a trailing slash)


$url = site_url( '/secrets/', 'https' );
echo $url;

Source Code
• site_url() is located in wp-includes/link-template.php.
6. get_site_url()
Retrieves the URL for a given site where WordPress
application files (e.g. wp-blog-header.php or the wp-admin/
folder) are accessible.

Description
• Returns the ‘site_url’ option with the appropriate protocol,
‘https’ if is_ssl() and ‘http’ otherwise.

• If $scheme is ‘http’ or ‘https’, is_ssl() is overridden.


6. get_site_url()
Parameters
• $blog_id
(int) (Optional) Site ID. Default null (current site).
Default value: null
• $path
(string) (Optional) Path relative to the site URL.
Default value: '‘
• $scheme
(string) (Optional) Scheme to give the site URL context.
Accepts 'http', 'https', 'login', 'login_post', 'admin', or
'relative'.
Default value: null
6. get_site_url()
Return
(string) Site URL link with optional path appended.

Example
<?php echo get_site_url(); ?>

Output:
• Results in the full site URL being displayed:
• http://www.example.com
• http://localhost/bca3/wordpress
5. Post tags
1. the_content()
Description
• The "the_content" filter is used to filter the content of the post
after it is retrieved from the database and before it is printed to
the screen.

• A plugin (or theme) can register as a content filter with the


code:

<?php add_filter( 'the_content', 'filter_function_name' ) ?>


1. the_content()
• Where 'filter_function_name' is the function WordPress should
call when the content is being retrieved.

• Note that the filter function must return the content after it is
finished processing, or site visitors will see a blank page and
other plugins also filtering the content may generate errors.

• filter_function_name should be unique function name. It


cannot match any other function name already declared.
1. the_content()
Parameters
• $more_link_text
(string) (Optional) Content for when there is more text.
Default value: null

• $strip_teaser
(bool) (Optional) Strip teaser content before the more text.
Default is false.
Default value: false
2. the_excerpt()
Description
• Returns the excerpt of the post. This is either a user-supplied
excerpt, that is returned unchanged, or an automatically
generated word-counted trimmed-down version of the full post
content.

• The auto-generated excerpts have all shortcodes and tags


removed which means it is just an unformatted string that
would not see any line-breaks in any form of output, since the
actual line-breaks in the raw text are also removed.

• When called through the_excerpt() this implies that the auto-


gen excerpts are just raw text with <p></p> tags wrapped
around it, basically.
2. the_excerpt()

The default word length is 55 words. It can be changed


through the excerpt_length filter.

<?php if ( is_category() || is_archive() )


{
the_excerpt();
}
else
{
the_content();
} ?>
3. the_ID()
Description
• Displays the numeric ID of the current post. This tag must be
within The Loop.

• Note: This function displays the ID of the post, to return the ID


use get_the_ID().

Usage
• <?php the_ID(); ?>

<p>Post Number: <?php the_ID(); ?></p>


3. the_ID()
Parameters
• This tag has no parameters.

Source File
• the_ID() is located in wp-includes/post-template.php.
4. the_tags()
Description
• This template tag displays a link to the tag or tags a post
belongs to.
• If no tags are associated with the current entry, nothing is
displayed. This tag should be used within The Loop.

Usage
• <?php the_tags( $before, $sep, $after ); ?>

Parameters
• $before
(string) Text to display before the actual tags are displayed.
Defaults to Tags:
4. the_tags()
• $sep
(string) Text or character to display between each tag link. The
default is a comma (,) between each tag.

• $after
(string) Text to display after the last tag. The default is to
display nothing.

Return Values
• None.
4. the_tags()
1. Default Usage
The default usage lists tags with each tag (if more than one)
separated by a comma (,) and preceded with the default text
Tags: .
<p><?php the_tags(); ?></p>

2. Separated by Commas
Displays a list of the tags with a line break after it.
<?php the_tags( 'Tags:', ‘ , ', '<br />' ); ?>

3. A List Example
Displays a list of the tags as an unordered list:
<?php the_tags( '<ul><li>', '</li><li>', '</li></ul>' ); ?>
4. the_tags()
4. Separated by Arrow
Displays links to tags with an arrow (>) separating the tags and
preceded with the text Social tagging:
<?php the_tags( 'Social tagging: ‘ , ' > ' ); ?>

5. Separated by a Bullet
Displays links to tags with a bullet (•) separating the tags and
preceded with the text Tagged with: and followed by a line
break.
<?php the_tags( 'Tagged with: ', ' • ', '<br />' ); ?>
5. the_title()
Description
• Display or retrieve the current post title with optional markup.

• This tag may only be used within The Loop, to get the title of a
post outside of the loop use get_the_title.

• If the post is protected or private, this will be noted by the


words "Protected: " or "Private: " prepended to the title.

Usage
• <?php the_title( $before, $after, $echo ); ?>
5. the_title()
Parameters
• $before
(string) (optional) Text to place before the title.
Default: None
• $after
(string) (optional) Text to place after the title.
Default: None
• $echo
(Boolean) (optional) Display the title (TRUE) or return it
for use in PHP (FALSE).
Default: TRUE
5. the_title()
Return
• (string|void) Current post title if $echo is false.

Example

<?php the_title( '<h3>', '</h3>' ); ?>

• This would print the title to the screen as an h3.


6. get_the_title()
Description
• If the post is protected and the visitor is not an admin, then
"Protected" will be displayed before the post title.

• If the post is private, then "Private" will be located before the


post title.

Parameters
• $post (int|WP_Post) (Optional) Post ID or WP_Post object.
Default is global $post.

Return
• (string)
6. get_the_title()
Example :
get_the_title(the_ID());
7. next_post_link()

• next_posts_link() usually points to older entries (toward the


end of the set).

• Used on single post permalink pages, this template tag


displays a link to the next post which exists in chronological
order from the current post.

• In standard usage (within the default, unaltered loop)


next_post_link will generate a link to a post that is newer (more
recent) than the current post.
7. next_post_link()
• This is in opposite to the similarly-named previous_posts_link,
which will typically link to a page of posts that is older than the
current batch.
Parameters
1. format
• (string) (Optional) Format string for the link. This is where
to control what comes before and after the link.
• '%link' in string will be replaced with whatever is declared
as 'link' (see next parameter).
• 'Go to %link' will generate "Go to <a href=..." Put HTML
tags here to style the final results.
• Default: '%link &raquo;
7. next_post_link()
2. link
(string) (Optional) Link text to display.
Default: '%title' (next post's title)

3. in_same_term
(boolean) (optional) Indicates whether next post must
be within the same taxonomy term as the current post. If
set to 'true', only posts from the current taxonomy term will
be displayed. If the post is in both the parent and
subcategory, or more than one term, the next post link will
lead to the next post in any of those terms. True, false

Default: false
7. next_post_link()
4. excluded_terms
(string/array) (optional) Array or a comma-separated list of
numeric terms IDs from which the next post should not be
listed. For example array(1, 5) or '1,5'. This argument used to
accept a list of IDs separated by 'and', this was deprecated in
WordPress 3.3
Default: None

5. taxonomy
(string) (Optional) Taxonomy, if $in_same_term is true. Added
in WordPress 3.8.
Default: 'category'
7. next_post_link()
1. Bold Post Title As Link
Displays link with next chronological post's title wrapped in
'strong' tags (typically sets text to bold).
<?php next_post_link( '<strong>%link</strong>' ); ?>

2. Text As Link, Without Post Title, Within Same Category


Displays custom text as link to the next post within the same
category as the current post.

Post title is not included here. "Next post in category" is the


custom text, which can be changed to fit your requirements.
Example:
<?php next_post_link( '%link', 'Next post in category', TRUE ); ?>
7. next_post_link()
3. Within Same Category, Excluding One
Displays link to next post in the same category, as long as it is
not in category 13 (the category ID #).

You can change the number to any category you wish to


exclude. Exclude multiple categories by using " and " as a
delimiter.

<?php
next_post_link( '%link', 'Next post in category', TRUE, '13' ); ?>
7. next_post_link()
4. Within Same Taxonomy
Displays link to next post in the same taxonomy term. Post
Formats are a taxonomy so the following would link to the next
post with the same post format.

<?php
next_post_link( '%link', 'Next post in taxonomy', TRUE, ' ',
'post_format' );
?>

Usage
<?php next_post_link( $format, $link, $in_same_term = false,
$excluded_terms = '', $taxonomy = 'category' ); ?>
8. previous_post_link()
• Used on single post permalink pages, this template tag
displays a link to the previous post which exists in
chronological order from the current post.
• This tag must be used in The Loop.

Parameters
• format
(string) (Optional) Format string for the link.

This is where to control what comes before and after the link.
'%link' in string will be replaced with whatever is declared as
'link' (see next parameter)
8. previous_post_link()
• 'Go to %link' will generate "Go to <a href=..." Put HTML tags
here to style the final results.
Default: '&laquo; %link‘

• link
(string) (Optional) Link text to display.
Default: '%title' (previous post's title)

• in_same_term
(boolean) (optional) Indicates whether previous post
must be within the same taxonomy term as the current post.
8. previous_post_link()

If set to 'true', only posts from the current taxonomy term will
be displayed.

If the post is in both the parent and subcategory, or more than


one term, the previous post link will lead to the previous post in
any of those terms.
true
false

• Default: false
8. previous_post_link()
• excluded_terms
(string/array) (optional) Array or a comma-separated list of
numeric terms IDs from which the next post should not be
listed.
For example array(1, 5) or '1,5'. This argument used to accept
a list of IDs separated by 'and', this was deprecated in
WordPress 3.3
Default: None

• taxonomy
(string) (Optional) Taxonomy, if $in_same_term is true. Added
in WordPress 3.8.
Default: 'category'
8. previous_post_link()
8. previous_post_link()
3. Text As Link, Without Post Title, Within Same Category
Displays custom text as link to the previous post within the
same category as the current post.
Post title is not included here. "Previous in category" is the
custom text, which can be changed to fit your requirements.

<?php
previous_post_link('%link', 'Previous in category', TRUE); ?>
8. previous_post_link()
4. Within Same Category, Excluding One
Array or comma-separated list of category ID(s) from which the
previous post should not be listed.
For example array( 1, 5) or '1,5'.
<?php

previous_post_link('%link', 'Previous in category', TRUE, '13');


?>

5. Within Same Taxonomy


Post Formats are a taxonomy so the following would link to the
previous post with the same post format.
<?
8. previous_post_link()
4. Post Title As Link, Within Same Custom Taxonomy
You have a Custom Post Type called Buildings, and a custom
taxonomy called Neighborhood.

<?php

previous_post_link( '%link', '%title', TRUE, ' ', 'neighborhood' );


?>
9. posts_nav_link()
• The first set of these site navigation links is featured only on
the non-single/non-permalink web pages, such as categories,
archives, searches, and the index page.

• It is the template tag posts_nav_link(). This tag creates two


links at the bottom of the page within the WordPress Loop to
display the next and previous pages in chronological order.

• By default, the posts_nav_link looks like this


<div class="navigation"><p>
<?php posts_nav_link(); ?></p></div>
9. posts_nav_link()
Parameters
1. $sep
(string) (Optional) Separator for posts navigation links.
Default value: ''
2. $prelabel
(string) (Optional) Label for previous pages.
Default value: ''
3. $nxtlabel
(string) (Optional) Label for next pages.
Default value: ''
9. posts_nav_link()

<?php posts_nav_link('separator','prelabel','nextlabel'); ?>

<div class="navigation">
<p><?php posts_nav_link('&#8734;','Go Forward In Time','Go
Back in Time'); ?> </p></div>
10. post_class()
• WordPress theme authors who want to have finer css control
options for their post styling, have the post_class function
available.

• When the post_class function is added to a tag within the loop,


for example <div <?php post_class(); ?> >, it will print out and
add various post-related classes to the div tag.

• It can also be used outside the loop with the optional post_id
parameter.

• This function is typically used in the index.php, single.php, and


other template files that feature hierarchical post listings.
10. post_class()
Parameters
class
(string or array) (optional) One or more classes to add to the
class attribute, separated by a single space.
Default: null

$post_id
(int) (optional) An optional post ID, used when calling this
function from outside The Loop Default: null
6. Post Thumbnail tags
1. has_post_thumbnail()
• Check if post has an image attached.

Parameters
• $post
(int|WP_Post) (Optional) Post ID or WP_Post object. Default is
global $post. Default value: null

Return
• (bool) Whether the post has an image attached.

Example
has_post_thumbnail( int|WP_Post $post = null )
2. get_post_thumbnail_id()
Description

1. If a featured image (formerly known as post thumbnail) is set -


Returns the ID of the featured image attached to the post

2. If no such attachment exists, the function returns an empty


string

3. If the post does not exist, the function returns false


2. get_post_thumbnail_id()
Note:
To enable featured images, see post thumbnails, the current
theme must include add_theme_support( 'post-thumbnails' );
in its functions.php file.

Usage
<?php
$post_thumbnail_id = get_post_thumbnail_id( $post_id );
?>
2. get_post_thumbnail_id()
Parameters
$post
(integer/WP_Post) (Optional) Post ID or WP_Post
object. If null, the current post will be used. Default: null

Return Values
(string) The ID of the post, or an empty string on failure.
2. get_post_thumbnail_id()
Notes
• "Post Thumbnail" is an outdated term for "Featured Image".

• This function returns the ID of the post's featured image.

• It does not return IDs of other images attached to posts that


are thumbnail sized.
3. the_post_thumbnail()
• Display the post thumbnail.

Description
• When a theme adds ‘post-thumbnail’ support, a special ‘post-
thumbnail’ image size is registered, which differs from the
‘thumbnail’ image size managed via the Settings > Media
screen.

• When using the_post_thumbnail() or related functions, the


‘post-thumbnail’ image size is used by default, though a
different size can be specified instead as needed.
3. the_post_thumbnail()
Parameters
• $size
(string|array) (Optional) Image size to use. Accepts any valid
image size, or an array of width and height values in pixels (in
that order).
Default value: 'post-thumbnail'

• $attr
(string|array) (Optional) Query string or array of attributes.
Default value: '‘
3. the_post_thumbnail()
Example

the_post_thumbnail( $size, $attr );

the_post_thumbnail( 'large','style=max-width:100%;height:auto;');
3. the_post_thumbnail()
• the_post_thumbnail();
// without parameter -> 'post-humbnail'
• the_post_thumbnail( 'thumbnail' );
// Thumbnail (default 150px x 150px max)
• the_post_thumbnail( 'medium' );
// Medium resolution (default 300px x 300px max)
• the_post_thumbnail( 'large' );
// Large resolution (default 640px x 640px max)
• the_post_thumbnail( 'full' );
// Full resolution (original size uploaded)
• the_post_thumbnail( array(100, 100) );
// Other resolutions
4. get_the_post_thumbnail()
• Retrieve the post thumbnail.

Description
• When a theme adds ‘post-thumbnail’ support, a special ‘post-
thumbnail’ image size is registered, which differs from the
‘thumbnail’ image size managed via the Settings > Media
screen.
• When using the_post_thumbnail() or related functions, the
‘post-thumbnail’ image size is used by default, though a
different size can be specified instead as needed.
4. get_the_post_thumbnail()
Parameters
1. $post
(int|WP_Post) (Optional) Post ID or WP_Post object. Default is
global $post. Default value: null

2. $size
(string|array) (Optional) Image size to use. Accepts any valid
image size, or an array of width and height values in pixels (in
that order). Default value: 'post-thumbnail'

3. $attr
(string|array) (Optional) Query string or array of attributes.
Default value: ''
4. get_the_post_thumbnail()
Return
• (string) The post thumbnail image tag.

More Information

• If the required add_theme_support( 'post-thumbnails' ); in the


current theme’s functions.php file is attached to a hook, it must
be must be called before the init hook is fired.

• The init hook may be too late for some features. If attached to a
hook, it must be after_setup_theme.
4. get_the_post_thumbnail()

get_the_post_thumbnail( int|WP_Post $post = null, string|


array $size = 'post-thumbnail', string|array $attr = '' )

<?php
echo get_the_post_thumbnail ( $post_id, 'thumbnail',
array( 'class' => 'alignleft' ) );
?>
7. Navigation Menu tags
Navigation Menus
• Navigation Menu is a theme feature introduced with
Version 3.0. WordPress includes an easy to use mechanism
for introducing customised navigation menus into a theme.

• In order to incorporate menu support into your theme, you


need to add a few code segments to your theme files.
Navigation Menus
Navigation Menus
Register Menus
• First, in your theme's functions.php, you need to write a
function to register the names of your menus.

• (This is how they will appear in the Appearance -> Menus


admin screen.) As an example, this menu would appear in
the "Theme Locations" box as "Header Menu".
Navigation Menus
Example:
functions.php

function ready_theme_menu()
{
register_nav_menu('primary','Main navigation');
}
add_action('init','ready_theme_menu');
Navigation Menus
Display Menus on Theme
• we might want our header menu to be in header.php.

• So open up that file in the theme editor, and decide where


you want to put your menu.

• The code to use here is wp_nav_menu which we will need


once for each menu location. So, add this code –

<?php
wp_nav_menu( array( 'theme_location' => 'header-menu' ) );
?>
wp_nav_menu()
Displays a navigation menu.

Parameters #Parameters
1. $args
(array) (Optional) Array of nav menu arguments.
'menu'
(int|string|WP_Term) Desired menu. Accepts
(matching in order) id, slug, name, menu object.

'menu_class'
(string) CSS class to use for the ul element which
forms the menu. Default 'menu'.
wp_nav_menu()
'menu_id'
(string) The ID that is applied to the ul element which
forms the menu. Default is the menu slug, incremented.

'container'
(string) Whether to wrap the ul, and what to wrap it with.
Default 'div'.

'container_class'
(string) Class that is applied to the container. Default
'menu-{menu slug}-container'.

'container_id'
(string) The ID that is applied to the container.
wp_nav_menu()
'before'
(string) Text before the link markup.
'after'
(string) Text after the link markup.
'link_before'
(string) Text before the link text.
'link_after'
(string) Text after the link text.
'echo'
(bool) Whether to echo the menu or return it. Default
true. 'depth'
(int) How many levels of the hierarchy are to be
included. 0 means all. Default 0.
wp_nav_menu()
'walker'
(object) Instance of a custom walker class.
'theme_location'
(string) Theme location to be used. Must be registered
with register_nav_menu() in order to be selectable by
the user.
'items_wrap'
(string) How the list items should be wrapped. Default is
a ul with an id and class. Uses printf() format with
numbered placeholders.
'item_spacing'
(string) Whether to preserve whitespace within the menu's
HTML. Accepts 'preserve' or 'discard'. Default 'preserve'.
wp_nav_menu()
Return
• (string|false|void) Menu output if $echo is false, false if there
are no items or no menu was found.
8. Conditional Tags
1. is_archive()
Description
• Is the query for an existing archive page?

• This Conditional Tag checks if any type of Archive page is


being displayed.

• An Archive is a Category, Tag, Author, Date, Custom Post,


Month, Year, Type or Custom Taxonomy based pages. This is
a boolean function, meaning it returns either TRUE or FALSE.

Usage
• <?php is_archive(); ?>
1. is_archive()
Return Values
• (boolean) True on success, false on failure.

NOTE:
Custom Post Types
• is_archive() does not accept any parameters. If you want to
check if this is the archive of a custom post type, use
is_post_type_archive( $post_type )
2. is_category()
• Is the query for an existing category archive page?
Description
• If the $category parameter is specified, this function will
additionally check if the query is for one of the categories
specified.

Parameters
• $category : (mixed) (Optional) Category ID, name, slug, or
array of Category IDs, names, and slugs.
• Default value: ''

Return
• (bool)
2. is_category()
• Example
is_category(array(36, 63, 64, 65, 67)
3. is_front_page()
Description
• This Conditional Tag checks if the main page is a posts or a
Page.

• This is a boolean function, meaning it returns either TRUE or


FALSE.

• It returns TRUE when the main blog page is being displayed


and the Settings->Reading->Front page displays is set to
"Your latest posts", or when is set to "A static page" and the
"Front Page" value is the current Page being displayed.
3. is_front_page()
Usage
• <?php is_front_page(); ?>

Parameters
• This tag does not accept any parameters.

Return Values
• (boolean) True on success, false on failure.
3. is_front_page()
• Examples
• If you are using a static page as your front page, this is useful:

<title> <?php bloginfo('name'); ?> »


<?php is_front_page() ? bloginfo('description') : wp_title(''); ?>
</title>
4. is_home()
• Determines if the query is for the blog homepage.

Description
• The blog homepage is the page that shows the time-based
blog content of the site.

• is_home() is dependent on the site’s "Front page displays"


Reading Settings ‘show_on_front’ and ‘page_for_posts’.

• If a static page is set for the front page of the site, this function
will return true only on the page you set as the "Posts page".
4. is_home()
Return
• (bool) True if blog view homepage, otherwise false.

Example
if ( is_home() ) {
// This is the blog posts index
get_sidebar( 'blog' );
} else {
// This is not the blog posts index
get_sidebar();
}
5. is_page()
• Is the query for an existing single page?

Description
• If the $page parameter is specified, this function will
additionally check if the query is for one of the pages specified.

Parameters
• $page (int | string | array) (Optional) Page ID, title, slug, or
array of such.
• Default value: '‘
5. is_page()
Return
• (bool) Whether the query is for an existing single page.
6. is_single()
• Is the query for an existing single post?

Description
• Works for any post type, except attachments and pages
• If the $post parameter is specified, this function will
additionally check if the query is for one of the Posts specified.

Parameters
• $post (int|string|array) (Optional) Post ID, title, slug, or array of
such.
• Default value: ''
6. is_single()
Return
• (bool) Whether the query is for an existing single post.

Example
s_single('17');
// When Post 17 (ID) is being displayed.
7. is_search()
• Is the query for a search?

Description
• This Conditional Tag checks if search result page archive is
being displayed. This is a boolean function, meaning it returns
either TRUE or FALSE.

Usage
• <?php is_search(); ?>
7. is_search()
Parameters
• This tag does not accept any parameters.

Return Values
• (boolean) True on success, false on failure.

Examples

<?php
if ( is_search() ) {
// add external search form (Google, Yahoo, Bing...)
}
?>
8. is_attachment()
Description
• This Conditional Tag checks if an attachment is being
displayed. An attachment is an image or other file uploaded
through the post editor's upload utility.

• Attachments can be displayed on their own 'page' or template.


For more information, see Using Image and File Attachments.

• This is a boolean function, meaning it returns either TRUE or


FALSE.
8. is_attachment()
Usage
• <?php is_attachment(); ?>

Return Values
• (boolean) True on success, false on failure.

Example
<?php
if ( is_attachment() ) {
// show adv. #1
} else {
// show adv. #2
} ?>
9. is_active_sidebar()
Description
• This Conditional Tag checks if a given sidebar is active (in
use). This is a boolean function, meaning it returns either
TRUE or FALSE.

• Any sidebar that contains widgets will return TRUE, whereas


any sidebar that does not contain any widgets will return
FALSE.

Usage
• <?php is_active_sidebar( $index ); ?>
9. is_active_sidebar()
Parameters
• $index (mixed) (required) Sidebar name or id.
• Default: None

Return Values
• (boolean) True if the sidebar is in use, otherwise the function
returns false
9. is_active_sidebar()
Examples
• Display different output depending on whether the sidebar is
active or not.

<?php
if ( is_active_sidebar( 'left-sidebar' ) )
{ ?>
<ul id="sidebar">
<?php dynamic_sidebar( 'left-sidebar' ); ?>
</ul>
<?php } ?>

You might also like