Unit 4
Unit 4
• 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(); ?>
<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.
• 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:
• 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.
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.
• 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'); ?>
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.
Parameters
• $sep
(string) (Optional) default is '»'. How to separate the various
items within the page title. Default value: '»'
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).
• 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
Description
• Will not display the comments template if not on single post or
page, or if the post does not have comments.
• Tries the $filtered path first and if it fails it will require the
default comment template from the default theme.
$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.
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' )
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()
• 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.
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.
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.
• Parameters
• $post-id (integer).
2. get_the_author()
Description
• Retrieve the post author's display name. This tag must
be used within The Loop.
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.
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.
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.
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.
• 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.
• 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( '> ' ); ?>
• Separated by a Bullet
• 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).
Usage
• <?php echo category_description( $category_id ); ?>
2. category_description()
Parameters
• $category_id (integer) (optional) The ID of the category to
return a description.
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()
Description
• Useful for category template files for displaying the category
page title.
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.
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".
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
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.
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
Output: https://localhost/bca3/wordpress/
Example 4
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.
Example:
<?php echo get_home_url(); ?>
Output: http://localhost/bca3/wordpress
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.
Default: null
Return
• (string) Site url link with optional path appended.
5. site_url()
Examples
• $url = site_url(); 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.
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.
• 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.
• $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.
Usage
• <?php the_ID(); ?>
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.
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
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()
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>' ); ?>
<?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: '« %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.
• 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
<?php
<div class="navigation">
<p><?php posts_nav_link('∞','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.
• It can also be used outside the loop with the optional post_id
parameter.
$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
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".
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.
• $attr
(string|array) (Optional) Query string or array of attributes.
Default value: '‘
3. the_post_thumbnail()
Example
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
• 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()
<?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.
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.
<?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?
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.
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:
Description
• The blog homepage is the page that shows the time-based
blog content of the site.
• 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.
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.
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 } ?>