0% found this document useful (0 votes)
17 views

Wordpress Ch-4

The document provides an overview of developing WordPress themes, detailing their structure, benefits, and essential files such as style.css and various template files. It emphasizes the importance of customization and the use of The Loop for displaying posts. Additionally, it explains key template tags and functions that facilitate theme development and integration within WordPress sites.

Uploaded by

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

Wordpress Ch-4

The document provides an overview of developing WordPress themes, detailing their structure, benefits, and essential files such as style.css and various template files. It emphasizes the importance of customization and the use of The Loop for displaying posts. Additionally, it explains key template tags and functions that facilitate theme development and integration within WordPress sites.

Uploaded by

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

CMS Using Word Press

Lec. Mr. Rakesh Kanani


Shree U.P.E.T. Mahila BCA College – Dhrol
www.realittricks.com
CS – 16: Content Management System using Word Press

1. Theme Development

Why WordPress Themes


WordPress Themes are files that work together to create the design and functionality of a WordPress
site. Each Theme may be different, offering many choices for site owners to instantly change their
website look.
You may wish to develop WordPress Themes for your own use, for a client project or to submit to the
WordPress Theme Directory. Why else should you build a WordPress Theme?
 To create a unique look for your WordPress site.
 To take advantage of templates, template tags, and the WordPress Loop to generate different
website results and looks.
 To provide alternative templates for specific site features, such as category pages and search
result pages.
 To quickly switch between two site layouts, or to take advantage of a Theme or style switcher to
allow site owners to change the look of your site.
A WordPress Theme has many benefits, too.
 It separates the presentation styles and template files from the system files so the site will
upgrade without drastic changes to the visual presentation of the site.
 It allows for customization of the site functionality unique to that Theme.
 It allows for quick changes of the visual design and layout of a WordPress site.
 It removes the need for a typical WordPress site owner to have to learn CSS, HTML, and PHP in
order to have a great-looking website.
Why should you build your own WordPress Theme? That's the real question.
 It's an opportunity to learn more about CSS, HTML, and PHP.
 It's an opportunity to put your expertise with CSS, HTML, and PHP to work.
 It's creative.
 It's fun (most of the time).
 If you release it to the public, you can feel good that you shared and gave something back to the
WordPress Community (okay, bragging rights)

Anatomy of a Theme
WordPress Themes live in subdirectories of the WordPress themes directory (wp-content/themes/ by
default) which cannot be directly moved using the wp-config.php file. The Theme's subdirectory holds
all of the Theme's stylesheet files, template files, and optional functions file (functions.php), JavaScript
files, and images. For example, a Theme named "test" would reside in the directory wp-
content/themes/test/. Avoid using numbers for the theme name, as this prevents it from being
displayed in the available themes list.
WordPress includes a default theme in each new installation. Examine the files in the default theme
carefully to get a better idea of how to build your own Theme files.
For a visual guide, see this infographic on WordPress Theme Anatomy.

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 2
CS – 16: Content Management System using Word Press
WordPress Themes typically consist of three main types of files, in addition to images and JavaScript
files.
1. The stylesheet called style.css, which controls the presentation (visual design and layout) of the
website pages.
2. WordPress template files which control the way the site pages generate the information from
your WordPress database to be displayed on the site.
3. The optional functions file (functions.php) as part of the WordPress Theme files.
Let's look at these individually.
WordPress themes are made up of a folder of template files, each of which controls a specific piece of
your theme. Parts of your site that remain static no matter what page you’re on are controlled by
header, sidebar and footer files. You can hack these files so they detect what page you are on and serve
different content accordingly, such as display different navigation on posts than on pages; however it is
most common for these sections to look the same throughout the site.
 header.php
Global file that displays headers and navigation. Also contains HTML code.
 The Loop
The display of contents of the main area of your site are controlled by individual WordPress
theme template files using what’s called “the loop”.
 sidebar.php
Sidebar display is controlled in this file. Multiple sidebars can be set up in functions.php, and
contents of sidebar widgets are set up from the WordPress wp-admin panel.
 footer.php
Contains instructions for global footer and closes HTML tags.

Theme Stylesheet
In addition to CSS style information for your theme, style.css provides details about the Theme in the
form of comments. The stylesheet must provide details about the Theme in the form of comments. No
two Themes are allowed to have the same details listed in their comment headers, as this will lead to
problems in the Theme selection dialog. If you make your own Theme by copying an existing one, make
sure you change this information first.
The following is an example of the first few lines of the stylesheet, called the stylesheet header, for the
Theme "Twenty Thirteen":

/*
Theme Name: Twenty Thirteen
Theme URI: http://wordpress.org/themes/twentythirteen
Author: the WordPress team
Author URI: http://wordpress.org/
Description: The 2013 theme for WordPress takes us back to the blog, featuring a full range of post
formats, each displayed beautifully in their own unique way. Design details abound, starting with a
vibrant color scheme and matching header images, beautiful typography and icons, and a flexible layout
that looks great on any device, big or small.

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 3
CS – 16: Content Management System using Word Press
Version: 1.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Tags: black, brown, orange, tan, white, yellow, light, one-column, two-columns, right-sidebar, flexible-
width, custom-header, custom-menu, editor-style, featured-images, microformats, post-formats, rtl-
language-support, sticky-post, translation-ready
Text Domain: twentythirteen

This theme, like WordPress, is licensed under the GPL.


Use it to make something cool, have fun, and share what you've learned with others.
*/
NB: The name used for the Author is suggested to be the same as the Theme Author's wordpress.org
username, although it can be the author's real name as well. The choice is the Theme Author's.
Note the list of Tags used to describe the theme. These allow user to find your theme using the tag
filter. You can find a full list in the Theme Review Handbook.
The comment header lines in style.css are required for WordPress to be able to identify the Theme and
display it in the Administration Panel under Design > Themes as an available Theme option along with
any other installed Themes.

Template Files
Templates are PHP source files used to generate the pages requested by visitors, and are output as
HTML. Template files are made up of HTML, PHP, and WordPress Template Tags.
Let's look at the various templates that can be defined as part of a Theme.
WordPress allows you to define separate templates for the various aspects of your site. It is not
essential, however, to have all these different template files for your site to fully function. Templates
are chosen and generated based upon the Template Hierarchy, depending upon what templates are
available in a particular Theme.
As a Theme developer, you can choose the amount of customization you want to implement using
templates. For example, as an extreme case, you can use only one template file, called index.php as the
template for all pages generated and displayed by the site. A more common use is to have different
template files generate different results, to allow maximum customization.

Template Files List


Here is the list of the Theme files recognized by WordPress. Of course, your Theme can contain any
other stylesheets, images, or files. Just keep in mind that the following have special meaning to
WordPress -- see Template Hierarchy for more information.
 style.css
The main stylesheet. This must be included with your Theme, and it must contain the
information header for your Theme.
 rtl.css
The rtl stylesheet. This will be included automatically if the website's text direction is right-to-
left. This can be generated using the RTLer plugin.

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 4
CS – 16: Content Management System using Word Press
 index.php
The main template. If your Theme provides its own templates, index.php must be present.
 comments.php
The comments template.
 front-page.php
The front page template.
 home.php
The home page template, which is the front page by default. If you use a static front page this is
the template for the page with the latest posts.
 single.php
The single post template. Used when a single post is queried. For this and all other query
templates, index.php is used if the query template is not present.
 single-{post-type}.php
The single post template used when a single post from a custom post type is queried. For
example, single-book.php would be used for displaying single posts from the custom post type
named "book". index.php is used if the query template for the custom post type is not present.
 page.php
The page template. Used when an individual Page is queried.
 category.php
The category template. Used when a category is queried.
 tag.php
The tag template. Used when a tag is queried.
 taxonomy.php
The term template. Used when a term in a custom taxonomy is queried.
 author.php
The author template. Used when an author is queried.
 date.php
The date/time template. Used when a date or time is queried. Year, month, day, hour, minute,
second.
 archive.php
The archive template. Used when a category, author, or date is queried. Note that this template
will be overridden by category.php, author.php, and date.php for their respective query types.
 search.php
The search results template. Used when a search is performed.

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 5
CS – 16: Content Management System using Word Press
 attachment.php
Attachment template. Used when viewing a single attachment.
 image.php
Image attachment template. Used when viewing a single image attachment. If not present,
attachment.php will be used.
 404.php
The 404 Not Found template. Used when WordPress cannot find a post or page that matches
the query.
These files have a special meaning with regard to WordPress because they are used as a replacement
for index.php, when available, according to the Template Hierarchy, and when the corresponding
Conditional Tag returns true. For example, if only a single post is being displayed, the is_single() function
returns 'true', and, if there is a single.php file in the active Theme, that template is used to generate the
page.

The Loop
The Loop is PHP code used by WordPress to display posts. Using The Loop, WordPress processes each
post to be displayed on the current page, and formats it according to how it matches specified criteria
within The Loop tags. Any HTML or PHP code in the Loop will be processed on each post.
When WordPress documentation says "This tag must be within The Loop", such as for specific Template
Tags or plugins, the tag will be repeated for each post. For example, The Loop displays the following
information by default for each post:
 Title (the_title())
 Time (the_time())
 Categories (the_category()).
You can display other information about each post using the appropriate Template Tags or (for
advanced users) by accessing the $post variable, which is set with the current post's information while
The Loop is running.

have_post()
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.

Usage
<?php have_post(); ?>

Parameters
This function does not accept any parameters.
Return Values
(Boolean)
True on success, false on failure.

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 6
CS – 16: Content Management System using Word Press
Example
<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post();
// Your loop code
endwhile;
else :
echo wpautop( 'Sorry, no posts were found' );
endif;
?>

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.

Sources
function the_post() {
global $wp_query;
$wp_query->the_post();
}

Basic Usage
if ( have_posts() ) {
while ( have_posts() ) {
the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php }
}

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 7
CS – 16: Content Management System using Word Press

Template Tag

1. General tags

wp_head()
The wp_head action hook is triggered within the <head></head> section of the user's template
by the wp_head() function. Although this is theme-dependent, it is one of the most essential
theme hooks, so it is widely supported.
This function is simply a wrapper for:

<?php
do_action('wp_head');
?>

This hook provides no parameters. You use this hook by having your function echo output to the
browser, or by having it perform background tasks. Your functions shouldn't return, and
shouldn't take any parameters.

This hook is theme-dependent which means that it is up to the author of each WordPress theme
to include it. It may not be available on all themes, so you should take this into account when
using it.

This hook is an action which means that it primarily acts as an event trigger, instead of a content
filter. This is a semantic difference, but it will help you to remember what this hook does if you
use it like this:

<?php
add_action('wp_head', 'your_function');
?>
Examples
Add custom css within head section using wp_head action.
function hook_css() {
?>
<style>
.wp_head_example {
background-color : #f1f1f1;
}
</style>
<?php
}
add_action('wp_head', 'hook_css');
Source File
wp_head action hook is located in wp-includes/general-template.php

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 8
CS – 16: Content Management System using Word Press
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.

Usage
<?php get_footer( $name ); ?>
Parameters
$name
(string) (optional) Calls for footer-name.php
Default: None.
Return Value
None

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 ); ?>
Parameters
$name
(string) (optional) Calls for header-name.php
Default: None.
Return Value
None

get_sidebar()
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

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 9
CS – 16: Content Management System using Word Press
Sources
function get_sidebar( $name = null ) {
/**
* Fires before the sidebar template file is loaded.
* The hook allows a specific sidebar template file to be used in place of the
* default sidebar template file. If your file is called sidebar-new.php,
* you would specify the filename in the hook as get_sidebar( 'new' ).
* @since 2.2.0
* @since 2.8.0 $name parameter added.
* @param string|null $name Name of the specific sidebar file to use. null for the default
sidebar.
*/
do_action( 'get_sidebar', $name );

$templates = array();
$name = (string) $name;
if ( '' !== $name )
$templates[] = "sidebar-{$name}.php";

$templates[] = 'sidebar.php';

locate_template( $templates, true );


}

get_search_form()
get_search_form( bool $echo = true )
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’.
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.
Parameters
$echo
(bool) (Optional) Default to echo and not return the form.
Default value: true
Return Value
(string|void) String when $echo is false.

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 10
CS – 16: Content Management System using Word Press
bloginfo()
bloginfo( string $show = '' )
Displays information about the current site.
Parameters
$show
(string) (Optional) Site information to display.
Default value: ''
Sources
function bloginfo( $show = '' ) {
echo get_bloginfo( $show, 'display' );
}
Displays information about your site, mostly gathered from the information you supply in your
User Profile and General Settings WordPress Administration Screens. It can be used anywhere
within a template file. This always prints a result to the browser. If you need the values for use in
PHP, use get_bloginfo().

Possible Values for $show


 name – Displays the “Site Title” set in Settings > General. This data is retrieved from the
“blogname” record in the wp_options table.
 ‘description‘ – Displays the “Tagline” set in Settings > General. This data is retrieved from
the “blogdescription” record in the wp_options table.
 ‘wpurl‘ – Displays the “WordPress address (URL)” set in Settings > General. This data is
retrieved from the “siteurl” record in the wp_options table. Consider echoing site_url()
instead, especially for multi-site configurations using paths instead of subdomains (it will
return the root site not the current sub-site).
 ‘url‘ – Displays the “Site address (URL)” set in Settings > General. This data is retrieved
from the “home” record in the wp_options table. Consider echoing home_url() instead.
 ‘admin_email‘ – Displays the “E-mail address” set in Settings > General. This data is
retrieved from the “admin_email” record in the wp_options table.
 ‘charset‘ – Displays the “Encoding for pages and feeds” set in Settings > Reading. This
data is retrieved from the “blog_charset” record in the wp_options table. Note: this
parameter always echoes “UTF-8”, which is the default encoding of WordPress.
 ‘version‘ – Displays the WordPress Version you use. This data is retrieved from the
$wp_version variable set in wp-includes/version.php.
 ‘html_type‘ – Displays the Content-Type of WordPress HTML pages (default:
“text/html”). This data is retrieved from the “html_type” record in the wp_options table.
Themes and plugins can override the default value using the pre_option_html_type filter.
 ‘text_direction‘ – Displays the Text Direction of WordPress HTML pages. Consider using
is_rtl() instead.
 ‘language‘ – Displays the language of WordPress.
 ‘stylesheet_url‘ – Displays the primary CSS (usually style.css) file URL of the active theme.
Consider echoing get_stylesheet_uri() instead.

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 11
CS – 16: Content Management System using Word Press
 ‘stylesheet_directory‘ – Displays the stylesheet directory URL of the active theme. (Was a
local path in earlier WordPress versions.) Consider echoing
get_stylesheet_directory_uri() instead.
 ‘template_url‘ / ‘template_directory‘ – URL of the active theme’s directory. Within child
themes, both get_bloginfo(‘template_url’) and get_template() will return the parent
theme directory. Consider echoing get_template_directory_uri() instead (for the parent
template directory) or get_stylesheet_directory_uri() (for the child template directory).
 ‘pingback_url‘ – Displays the Pingback XML-RPC file URL (xmlrpc.php).
 ‘atom_url‘ – Displays the Atom feed URL (/feed/atom).
 ‘rdf_url‘ – Displays the RDF/RSS 1.0 feed URL (/feed/rfd).
 ‘rss_url‘ – Displays the RSS 0.92 feed URL (/feed/rss).
 ‘rss2_url‘ – Displays the RSS 2.0 feed URL (/feed).
 ‘comments_atom_url‘ – Displays the comments Atom feed URL (/comments/feed).
 ‘comments_rss2_url‘ – Displays the comments RSS 2.0 feed URL (/comments/feed).
 ‘siteurl‘ – Deprecated since version 2.2. Echo home_url(), or use bloginfo(‘url’).
 ‘home‘ – Deprecated since version 2.2. Echo home_url(), or use bloginfo(‘url’).

wp_title()
wp_title( string $sep = '&raquo;', bool $display = true, string $seplocation = '' )
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.
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;'
$display
(bool) (Optional) Whether to display or retrieve title.
Default value: true
$seplocation
(string) (Optional) Direction to display title, 'right'.
Default value: ''
Return Value
(string|null) String on retrieve, null when displaying.

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 12
CS – 16: Content Management System using Word Press
Single_post_title()
single_post_title( string $prefix = '', bool $display = true )

Display or retrieve page title for post.


This is optimized for single.php template file for displaying the post title.
It does not support placing the separator after the title, but by leaving the prefix parameter
empty, you can set the title separator manually. 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.
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.

wp_footer()
Fire the 'wp_footer' action. Put this template tag immediately before </body> tag in a theme
template (ex. footer.php, index.php).
Usage
<?php wp_footer(); ?>
Parameter
This function does not accept any parameters.
Example
...
<?php
/* Always have wp_footer() just before the closing </body>
* tag of your theme, or you will break many plugins, which
* generally use this hook to reference JavaScript files.
*/
wp_footer();
?>
</body>
</html>

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 13
CS – 16: Content Management System using Word Press
comments_template()
comments_template( string $file = '/comments.php', bool $separate_comments = false )

Load the comment template specified in $file.


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.

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.

Will not try to get the comments if the post has none.

Parameter
$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

add_theme_support()
add_theme_support( string $feature )

Registers theme support for a given feature.


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.
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.
Return
(void|bool) False on failure, void otherwise.

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 14
CS – 16: Content Management System using Word Press

get_template_directory_uri()
get_template_directory_uri()

Retrieve theme directory URI.


Return
(string) Template directory URI.

Body_class()
The "body_class" filter is used to filter the classes that are assigned to the body HTML element
on the current page.

A plugin (or theme) can filter these classes with the code:

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

Where 'filter_function_name' is the function WordPress should call when the classes are being
assigned. 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.

This filter is used by the get_body_class() function.

Example
// Apply filter
add_filter('body_class', 'multisite_body_classes');

function multisite_body_classes($classes) {
$id = get_current_blog_id();
$slug = strtolower(str_replace(' ', '-', trim(get_bloginfo('name'))));
$classes[] = $slug;
$classes[] = 'site-id-'.$id;
return $classes;
}

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 15
CS – 16: Content Management System using Word Press
2. Author tags

the_author()
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(); ?>
Example
Displays the value in the user's Display name publicly as field.

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


Parameter
$post-id (integer).

get_the_author()
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.

Since WordPress 2.1 parameters are deprecated (not the function).


Usage
<?php $author = get_the_author(); ?>
Parameters
$deprecated
(string) (optional) Deprecated.
Default: ''
Example
Grabs the value in the user's Display name publicly as field.
<?php $author = get_the_author(); ?>

the_author_link()
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.
Usage
<?php the_author_link(); ?>

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 16
CS – 16: Content Management System using Word Press
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>
Which displays as:

Written by: James Smith

get_the_author_link()
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.

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.
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>

Which displays as:

Written by: James Smith

the_author_meta()
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.
Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 17
CS – 16: Content Management System using Word Press
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:
 user_login
 user_pass
 user_nicename
 user_email
 user_url
 user_registered
 user_activation_key
 user_status
 display_name
 nickname
 first_name
 last_name
 description
 jabber
 aim
 yim
 user_level
 user_firstname
 user_lastname
 user_description
 rich_editing
 comment_shortcuts
 admin_color
 plugins_per_page
 plugins_last_view
 ID
$userID
(integer) (optional) If the user ID fields is used, then this function display the specific field for this
user ID.
Default: false
Example
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>

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 18
CS – 16: Content Management System using Word Press
the_author_posts()
Displays the total number of posts an author has published. Drafts and private posts aren't
counted. This tag must be used within The Loop.
Usage
<?php the_author_posts(); ?>

Example
Displays the author's name and number of posts.

<p><?php the_author(); ?> has blogged <?php the_author_posts(); ?>


posts</p>

Harriett Smith has blogged 425 posts.

Parameters
This tag does not accept any parameters.

3. Category tags

category_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 ); ?>
Parameters
$category_id
(integer) (optional) The ID of the category to return a description.
Default: Description of current query category.
Example
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>

Result:
WordPress is a favorite blogging tool of mine and I share tips and tricks for using WordPress
here.

Note: if there is no category description, the function returns a br tag.

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 19
CS – 16: Content Management System using Word Press
single_cat_title()
single_cat_title( string $prefix = '', bool $display = true )

Display or retrieve page title for category archive.


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.
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.

the_category()
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
$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
Note: Default is a link to the child category, with no relationship exhibited.
$post_id
(int) (optional) Post ID to retrieve categories. The default value false results in the category list of
the current post.
Default: false

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 20
CS – 16: Content Management System using Word Press
Example
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( ', ' ); ?>

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;' ); ?>

4. Link Tags

the_permalink()
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.
Usage
<?php the_permalink(); ?>
Parameter
Before 4.4.0, this tag has no parameters. Since 4.4.0: Added the `$post` parameter.
Example
Display Post URL as Text
Displays the URL to the post, without creating a link:
This address for this post is: <?php the_permalink(); ?>
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>

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 21
CS – 16: Content Management System using Word Press
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.
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title();
?></a>

get_permalink()
get_permalink( int|WP_Post $post, bool $leavename = false )

Retrieves the full permalink for the current post or post ID.
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
Return
(string|false) The permalink URL or false if post does not exist.

home_url()
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.

In case of WordPress Network Setup, use network_home_url() instead.


Usage
<?php home_url( $path, $scheme ); ?>
Default Usage
<?php echo esc_url( 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

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 22
CS – 16: Content Management System using Word Press
Return
(string)
Home URL with the optional $path argument appended.
Example
$url = home_url();
echo esc_url( $url );
Output: http://www.example.com

(Note the lack of a trailing slash)

$url = home_url( '/' );


echo esc_url( $url );
Output: http://www.example.com/

$url = home_url( '/', 'https' );


echo esc_url( $url );
Output: https://www.example.com/

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


echo esc_url( $url );
Output: /example

get_home_url()
get_home_url( int $blog_id = null, string $path = '', string|null $scheme = null )

Retrieves the URL for a given site where the front end is accessible.
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.
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
Return
(string) Home URL link with optional path appended.

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 23
CS – 16: Content Management System using Word Press
site_url()
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.

In case of WordPress Network setup, use network_site_url() instead.


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
$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.
get_site_url()
get_site_url( int $blog_id = null, string $path = '', string $scheme = null )
Retrieves the URL for a given site where WordPress application files (e.g. wp-blog-header.php or
the wp-admin/ folder) are accessible.
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.
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
Return
(string) Site URL link with optional path appended.

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 24
CS – 16: Content Management System using Word Press
5. Post Tags

the_content()
the_content( string $more_link_text = null, bool $strip_teaser = false )

Display the post 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
Sources
function the_content( $more_link_text = null, $strip_teaser = false) {
$content = get_the_content( $more_link_text, $strip_teaser );

/**
* Filters the post content.
*
* @since 0.71
*
* @param string $content Content of the current post.
*/
$content = apply_filters( 'the_content', $content );
$content = str_replace( ']]>', ']]&gt;', $content );
echo $content;
}

the_excerpt()
the_excerpt()

Display the post excerpt.


The WordPress Excerpt is an optional summary or description of a post; in short, a post
summary.
The Excerpt has two main uses:
1. It replaces the full content in RSS feeds when the option to display summaries is selected
in Dashboard › Settings › Reading.
2. Depending on the WordPress theme, it can be displayed in places where quick
summaries are preferable to full content:
 Search results
 Tag archives
 Category archives
 Monthly archives
 Author archives

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 25
CS – 16: Content Management System using Word Press
Sources
function the_excerpt() {

/**
* Filters the displayed post excerpt.
*
* @since 0.71
*
* @see get_the_excerpt()
*
* @param string $post_excerpt The post excerpt.
*/
echo apply_filters( 'the_excerpt', get_the_excerpt() );
}

the_ID()
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(); ?>
Parameter
This tag has no parameters.
Example
Default Usage
<p>Post Number: <?php the_ID(); ?></p>
Post Anchor Identifier
Provides a unique anchor identifier to each post:
<h3 id="post-<?php the_ID(); ?>"><?php the_title(); ?></h3>
Note: In XHTML, the id attribute must not start with a digit. Since the_ID returns the post ID as
numerical data, you should include at least one alphabetical character before using it in an id
attribute, as in the example above.

the_tags()
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 ); ?>

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 26
CS – 16: Content Management System using Word Press
Parameters
$before
(string) Text to display before the actual tags are displayed. Defaults to 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 Value
None

Example
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>


Separated by Commas
Displays a list of the tags with a line break after it.

<?php the_tags( 'Tags: ', ', ', '<br />' ); ?>


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: ',' > ' ); ?>


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 />' ); ?>
A List Example
Displays a list of the tags as an unordered list:

<?php the_tags( '<ul><li>', '</li><li>', '</li></ul>' ); ?>

the_title()
Displays or returns the unescaped title of the current post. 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 ); ?>

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 27
CS – 16: Content Management System using Word Press
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
Example
<?php the_title( '<h3>', '</h3>' ); ?>
This would print the title to the screen as an h3.

get_the_title()
get_the_title( int|WP_Post $post )

Retrieve post title.


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

the_date()
Displays or returns the date of a post, or a set of posts if published on the same day.

SPECIAL NOTE: When there are multiple posts on a page published under the SAME DAY,
the_date() only displays the date for the first post (that is, the first instance of the_date()). To
repeat the date for posts published under the same day, you should use the Template Tag
the_time() or get_the_date() (since 3.0) with a date-specific format string.
Use <?php the_time( get_option( 'date_format' ) ); ?> to add the date set in the admin interface.
This tag must be used within The Loop.
Usage
<?php the_date( $format, $before, $after, $echo ); ?>
Parameter
$format
(string) (optional) The format for the date. Defaults to the date format configured in your
WordPress options. See Formatting Date and Time.
Default: F j, Y

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 28
CS – 16: Content Management System using Word Press
$before
(string) (optional) Text to place before the date.
Default: None
$after
(string) (optional) Text to place after the date
Default: None
$echo
(boolean) (optional) Display the date (TRUE), or return the date to be used in PHP (FALSE).
Default: TRUE
Return
(string|null) Null if displaying, string if retrieving.
Example
Default Usage
Displays the date using defaults.
<?php the_date(); ?>
Date as Year, Month, Date in Heading
Displays the date using the '2007-07-23' format (ex: 2004-11-30), inside an <h2> tag.
<?php the_date('Y-m-d', '<h2>', '</h2>'); ?>
Date in Heading Using $my_date Variable
Returns the date in the default format inside an <h2> tag and assigns it to the $my_date
variable. The variable's value is then displayed with the PHP echo command.
<?php $my_date = the_date('', '<h2>', '</h2>', FALSE); echo $my_date; ?>
Notes
Affects the return value of the is_new_day() function.

get_the_date()
The get_the_date template tag retrieves the date the current $post was written. Unlike
the_date() this tag will always return the date. Modify output with 'get_the_date' filter.

Usage
<?php $pfx_date = get_the_date( $format, $post_id ); ?>
Parameters
$format
(string) (optional) PHP date format.
Default: the date_format option ('Date Format' on Settings > General panel)
$post
(integer) (optional) The ID of the post you'd like to fetch. By default the current post is fetched.
Default: null

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 29
CS – 16: Content Management System using Word Press
Return
(string) The formatted date string
Example
Default Usage
<span class="entry-date"><?php echo get_the_date(); ?></span>

the_time()
Displays the time of the current post. To return the time of a post, use get_the_time(). This tag
must be used within The Loop.

Usage
<?php the_time( $d ); ?>
Parameters
$d
(string) (optional) The format the time is to display in. Defaults to the time format configured in
your WordPress options. See Formatting Date and Time.
Default: None
Example
Default Usage
Displays the time using your WordPress defaults.
<p>Time posted: <?php the_time(); ?></p>

Time as AM/PM VS. 24H format


Displays the time using the format parameter string 'g:i a' (ex: 10:36 pm).
<p>Time posted: <?php the_time('g:i a'); ?></p>
Displays the time using the 24 hours format parameter string 'G:i' (ex: 17:52).
<p>Time posted: <?php the_time('G:i'); ?></p>
Date as Month Day, Year
Displays the time in the date format 'F j, Y' (ex: December 2, 2004), which could be used to
replace the tag the_date().
<div><?php the_time('F j, Y'); ?></div>
Date and Time
Displays the date and time.
<p>Posted: <?php the_date('F j, Y'); ?> at <?php the_time('g:i a'); ?></p>
Posted: July 17, 2007 at 7:19 am

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 30
CS – 16: Content Management System using Word Press
next_post_link()
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. This is in contrary to the similarly-named
previous_posts_link, which will typically link to a page of posts that is older than the current
batch.

This tag must be used in The Loop.

Usage
<?php next_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $taxonomy
= 'category' ); ?>

Parameter
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;'
link
(string) (Optional) Link text to display.
Default: '%title' (next post's title)
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
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'
Example
Default Usage
Displays link with the post title of the next post (chronological post date order), followed by a
right angular quote (»).

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 31
CS – 16: Content Management System using Word Press
Next Post Title »

<?php next_post_link(); ?>


Bold Post Title As Link
Displays link with next chronological post's title wrapped in 'strong' tags (typically sets text to
bold).

Next Post Title

<?php next_post_link( '<strong>%link</strong>' ); ?>


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.

Next post in category

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


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.

Next post in category

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


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.

Next post in taxonomy

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

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.


Arguments

<?php previous_post_link( $format, $link, $in_same_term = false, $excluded_terms = '',


$taxonomy = 'category' ); ?>

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 32
CS – 16: Content Management System using Word Press
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). '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. 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
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'
Example
Default Usage
Displays link with left angular quote («) followed by the post title of the previous post
(chronological post date order).

« Previous Post Title

<?php previous_post_link(); ?>

Bold Post Title As Link


Displays link with previous chronological post's title wrapped in 'strong' tags (typically sets text
to bold).

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 33
CS – 16: Content Management System using Word Press

Previous Post Title

<?php previous_post_link('<strong>%link</strong>'); ?>

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.

Previous in category

<?php previous_post_link('%link', 'Previous in category', TRUE); ?>

Within Same Category, Excluding One


Displays link to previous 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. 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'.

Previous in category

<?php previous_post_link('%link', 'Previous in category', TRUE, '13'); ?>

Within Same Taxonomy


Displays link to previous post in the same taxonomy term. Post Formats are a taxonomy so the
following would link to the previous post with the same post format.

Previous post in taxonomy

<?php previous_post_link( '%link', 'Previous post in category', TRUE, ' ', 'post_format' ); ?>

Post Title As Link, Within Same Custom Taxonomy


Displays link to previous post in the same custom taxonomy term. You have a Custom Post Type
called Buildings, and a custom taxonomy called Neighborhood. Here you don't need to use the
Custom Post Type( as Custom Post Type already in the WP query). Just mention the taxonomy
name(which is neighborhood as per this example) as last parameter of previous_post_link
function.

Previous (Custom) Post Title in Neighborhood Taxonomy

<?php previous_post_link( '%link', '%title', TRUE, ' ', 'neighborhood' ); ?>

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 34
CS – 16: Content Management System using Word Press
posts_nav_link()
Displays links for next and previous pages. Useful for providing "paged" navigation of index,
category and archive pages.
For displaying next and previous pages of posts see next_posts_link() and previous_posts_link().
For displaying next and previous post navigation on individual posts, see next_post_link() and
previous_post_link().

Usage
<?php posts_nav_link( $sep, $prelabel, $nextlabel ); ?>
Note: since weblog posts are traditionally listed in reverse chronological order (with most recent
posts at the top), there is some ambiguity in the definition of "next page". WordPress defines
"next page" as the "next page toward the past". In WordPress 1.5, the default Kubrick theme
addresses this ambiguity by labeling the "next page" link as "previous entries". See Example:
Kubrick Theme Format.
Parameters
$sep
(string) Text displayed between the links.
 Defaults to ' :: ' in 1.2.x.
 Defaults to ' — ' in 1.5.
$prelabel
(string) Link text for the previous page.
 Defaults to '<< Previous Page' in 1.2.x.
 Defaults to '« Previous Page' in 1.5.
$nxtlabel
(string) Link text for the next page.
 Defaults to 'Next Page >>' in 1.2.x.
 Defaults to 'Next Page »' in 1.5

Example
Default Usage
By default, the posts_nav_link() look like this:

« Previous Page — Next Page »

<?php posts_nav_link(); ?>


In Centered DIV
Displays previous and next page links ("previous page · next page") centered on the page.
<div style="text-align:center;">
<?php posts_nav_link( ' &#183; ', 'previous page', 'next page' ); ?>
</div>

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 35
CS – 16: Content Management System using Word Press
Using Images
<?php posts_nav_link( ' ', '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/prev.jpg"
/>', '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/next.jpg" />' ); ?>
Kubrick Theme Format
The Kubrick theme format for posts navigation, could be formatted this way. However, using
posts_nav_link() in this way will result in unintended behavior, such as double stacked next and
previous links that link to the incorrect sections.
The Kubrick Theme actually uses next_posts_link() and previous_posts_link().
This is poor code and should not be used:
<div class="navigation">
<div class="alignleft"><?php posts_nav_link( '', '', '&laquo; Previous Entries' ); ?></div>
<div class="alignright"><?php posts_nav_link( '', 'Next Entries &raquo;', '' ); ?></div>
</div>
This is better code:
<div class="navigation">
<div class="alignleft"><?php previous_posts_link( '&laquo; Previous Entries' ); ?></div>
<div class="alignright"><?php next_posts_link( 'Next Entries &raquo;', '' ); ?></div>
</div>
Customizing the Link Text
You can change the text in each of the links and in the text in between the links.

You can go back to the previous page or you can go forward to the next page.

<p><?php posts_nav_link( ' or ', 'You can go back to the previous page', 'you can go forward to
the next page' ); ?>.</p>

posts_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.

If you would prefer to have the post classes returned instead of echoed, you would want to use
get_post_class(). Note: get_post_class() does not return a string, but an array that must be
processed to produce text similar to what is echoed by post_class().

For css classes intended to help target entire pages, see body_class(), and for classes targeting
comment listings, see comment_class().

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 36
CS – 16: Content Management System using Word Press
Usages

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


The post_class may include one or more of the following values for the class attribute,
dependent upon the pageview.

 .post-[id]
 .[post-type]
 .type-[post-type]
 .status-[post-status]
 .format-[post-format] (default to 'standard')
 .post-password-required
 .post-password-protected
 .has-post-thumbnail
 .sticky
 .hentry (hAtom microformat pages)
 .[taxonomy]-[taxonomy-slug] (includes category)
 .tag-[tag-name]

Default Values
The post_class CSS classes appear based upon the post pageview Conditional Tags as follows.

Front Page
If posts are found on the front page of the blog, be it static or not, the class selectors are: post
post-id hentry

Single Post
Single post template files and pageviews feature the class selectors: post post-id hentry

Sticky Post
Posts designated as "sticky," sticking to the front page of the blog, feature the class selector:
sticky

Author
Author template files and pageviews displaying posts feature the class selectors: post post-id

Category
Category template files and pageviews displaying posts feature the class selectors: post post-id
category-ID category-name

Tags
Tag template files and pageviews with posts feature the class selectors: tag-name post post-id

Archives
Archive pageviews and pageviews with posts feature CSS classes: post post-id

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 37
CS – 16: Content Management System using Word Press
Search
Search template files and pageviews with posts feature the class selectors: post post-id

Attachment
Attachment template files and pageviews feature the class selectors: attachment post post-id

Format
Posts using Post Formats feature the class selector: format-name

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

Example
The following example shows how to implement the post_class template tag into a theme.

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


The actual HTML output might resemble something like this for a post in the "dancing" category:

<div id="post-4564" class="post post-4564 category-48 category-dancing logged-in">


In the WordPress Theme stylesheet, add the appropriate styles, such as:

.post {
/* styles for all posts */
}
.post-4564 {
/* styles for only post ID number 4564 */
}
.category-dancing {
/* styles for all posts within the category of dancing */
}

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 38
CS – 16: Content Management System using Word Press
6. Post Thumbnail Tags

has_post_thumbnail()
has_post_thumbnail( int|WP_Post $post = null )

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.

Sources
function has_post_thumbnail( $post = null ) {
return (bool) get_post_thumbnail_id( $post );
}
get_post_thumbnail_id()
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
Note: To enable featured images, see post thumbnails, the current theme must include
add_theme_support( 'post-thumbnails' ); in its functions.php file. See also Post Thumbnails.
Usage
<?php $post_thumbnail_id = get_post_thumbnail_id( $post_id ); ?>
Parameters
$post
(integer/WP_Post) (Optional) Post ID or WP_Post object. If null, the current post will be used.
Default: null
Return Value
(string)
The ID of the post, or an empty string on failure.

the_post_thumbnail()
the_post_thumbnail( string|array $size = 'post-thumbnail', string|array $attr = '' )
Display the post thumbnail.
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.

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 39
CS – 16: Content Management System using Word Press
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: ''
Source

function the_post_thumbnail( $size = 'post-thumbnail', $attr = '' ) {


echo get_the_post_thumbnail( null, $size, $attr );
}

get_the_post_thumbnail()
get_the_post_thumbnail( int|WP_Post $post = null, string|array $size = 'post-thumbnail',
string|array $attr = '' )

Retrieve the post thumbnail.


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.
Parameters
$post
(int|WP_Post) (Optional) Post ID or WP_Post object. Default is global $post.
Default value: null
$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: ' '
Return Value
(string) The post thumbnail image tag.

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 40
CS – 16: Content Management System using Word Press
7. Navigation Menu Tags

wp_nav_menu()
wp_nav_menu( array $args = array() )

Displays a navigation menu.

Parameters
$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'.
 '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.
 'fallback_cb'
(callable|bool) If the menu doesn't exists, a callback function will fire. Default is 'wp_page_menu'. Set to
false for no fallback.
 '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.
 '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'.
Default value: array()

Return Value
(string|false|void) Menu output if $echo is false, false if there are no items or no menu was found.

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 41
CS – 16: Content Management System using Word Press
More Information #More Information
Usage #Usage

1 wp_nav_menu( $args );

Given a theme_location parameter, the function displays the menu assigned to that location. If no such location
exists or no menu is assigned to it, the parameter fallback_cb will determine what is displayed.
If not given a theme_location parameter, the function displays
 the menu matching the ID, slug, or name given by the menu parameter;
 otherwise, the first non-empty menu;
 otherwise (or if the menu given by menu is empty), output of the function given by the fallback_cb parameter
(wp_page_menu(), by default);
 otherwise nothing.

Menu Item CSS Classes #Menu Item CSS Classes


The following classes are applied to menu items, i.e. to the HTML <li> tags, generated by wp_nav_menu():
All Menu Items #All Menu Items
 .menu-item
This class is added to every menu item.
 .menu-item-has-children
This class is added to menu item which has sub-items .
 .menu-item-object-{object}
This class is added to every menu item, where {object} is either a post type or a taxonomy.
 .menu-item-object-category
This class is added to menu items that correspond to a category.
 .menu-item-object-tag
This class is added to menu items that correspond to a tag.
 .menu-item-object-page
This class is added to menu items that correspond to static pages.
 .menu-item-object-{custom}
This class is added to menu items that correspond to a custom post type or a custom taxonomy.
 .menu-item-type-{type}
This class is added to every menu item, where {type} is either “post_type” or “taxonomy”.
 .menu-item-type-post_type
This class is added to menu items that correspond to post types: i.e. static pages or custom post types.
 .menu-item-type-taxonomy
This class is added to menu items that correspond to taxonomies: i.e. categories, tags, or custom taxonomies.
Top ↑
Current-Page Menu Items #Current-Page Menu Items
 .current-menu-item
This class is added to menu items that correspond to the currently rendered page.
Top ↑
Current-Page Parent Menu Items #Current-Page Parent Menu Items
 .current-menu-parent
This class is added to menu items that correspond to the hierarchical parent of the currently rendered page.
 .current-{object}-parent
This class is added to menu items that correspond to the hierachical parent of the currently rendered object, where
{object} corresponds to the the value used for .menu-item-object-{object}.
 .current-{type}-parent
This class is added to menu items that correspond to the hierachical parent of the currently rendered type, where
{type} corresponds to the the value used for .menu-item-type-{type}.
Current-Page Ancestor Menu Items #Current-Page Ancestor Menu Items
 .current-menu-ancestor
This class is added to menu items that correspond to a hierarchical ancestor of the currently rendered page.
 .current-{object}-ancestor
This class is added to menu items that correspond to a hierachical ancestor of the currently rendered object, where
{object} corresponds to the the value used for .menu-item-object-{object}.

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 42
CS – 16: Content Management System using Word Press
 .current-{type}-ancestor
This class is added to menu items that correspond to a hierachical ancestor of the currently rendered type, where
{type} corresponds to the the value used for .menu-item-type-{type}.
Top ↑
Site Front Page Menu Items #Site Front Page Menu Items
 .menu-item-home
This class is added to menu items that correspond to the site front page.
Top ↑
Backward Compatibility with wp_page_menu() #Backward Compatibility withwp_page_menu()
The following classes are added to maintain backward compatibility with the [[Function
Reference/wp_page_menu|wp_page_menu()]] function output:
 .page_item
This class is added to menu items that correspond to a static page.
 .page_item_has_children
This class is added to menu items that have sub pages to it.
 .page-item-$ID
This class is added to menu items that correspond to a static page, where $ID is the static page ID.
 .current_page_item
This class is added to menu items that correspond to the currently rendered static page.
 .current_page_parent
This class is added to menu items that correspond to the hierarchical parent of the currently rendered static page.
 .current_page_ancestor
This class is added to menu items that correspond to a hierarchical ancestor of the currently rendered static page.

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 43
CS – 16: Content Management System using Word Press
8. Conditional Tags

Is_archive()
Description
This Conditional Tag checks if any type of Archive page is being displayed. An Archive is a Category, Tag, Author, Date, Custom Post
Type or Custom Taxonomy based pages. This is a boolean function, meaning it returns either TRUE or FALSE.

Usage
<?php is_archive(); ?>

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

Examples
<?php
if ( is_archive() ) {
// write your code here ...
}
?>

Notes
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 )

Is_category()
is_category( mixed $category = '' )
Is the query for an existing category archive page?

Description #Description
If the $category parameter is specified, this function will additionally check if the query is for one of the categories specified.

Parameters #Parameters
$category

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

Top ↑

Return #Return
(bool)

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 44
CS – 16: Content Management System using Word Press
Is_front_page()
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.

Usage
<?php is_front_page(); ?>

Parameters
This tag does not accept any parameters.

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

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>

Usage in a Custom Function

Added to your themes functions file, this code includes the is_front_page() conditional tag after the function name so the content only displays
on the front page.

add_action( 'loop_start', 'using_front_page_conditional_tag' );

function using_front_page_conditional_tag() {

if ( is_front_page() ) {

echo'<h2>Only Displays On The Front Page</h2>';

Is_home()
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".
Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 45
CS – 16: Content Management System using Word Press
Return #Return
(bool) True if blog view homepage, otherwise false.

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

Parameters #Parameters
$page

(int|string|array) (Optional) Page ID, title, slug, or array of such.


Default value: ''

Return #Return
(bool) Whether the query is for an existing single page.

Is_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(); ?>

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...)
}
?>

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 46
CS – 16: Content Management System using Word Press
Function.php
ne way to change the default behaviors of WordPress is using a file named functions.php. It goes in your Theme's folder.

The functions file behaves like a WordPress Plugin, adding features and functionality to a WordPress site. You can use it to call functions, both
PHP and built-in WordPress, and to define your own functions. You can produce the same results by adding code to a WordPress Plugin or
through the WordPress Theme functions file.

There are differences between the two.

A WordPress Plugin:

 Requires specific, unique Header text.


 Is stored in wp-content/plugins, usually in a subdirectory.
 Executes only when individually activated, via the Plugins panel.
 Applies to all themes.
 Should have a single purpose, e.g., convert posts to Pages, offer search engine optimization features, or help with backups.
A functions file:

 Requires no unique Header text.


 Is stored with each Theme in the Theme's subdirectory in wp-content/themes.
 Executes only when in the currently activated theme's directory.
 Applies only to that theme. If the Theme is changed, the functionality is lost.
 Can have numerous blocks of code used for many different purposes.
Each theme has its own functions file, but only the functions.php in the active Theme affects how your site publicly displays. If your
theme already has a functions file, you can add code to it. If not, you can create a plain-text file named functions.php to add to your
theme's directory.

A Child Theme can have its own functions.php. This child functions file can be used to augment or replace the parent theme's
functions.

With a functions file you can:

 Use WordPress Hooks, that vast collection of WordPress actions and filters that can alter almost everything WordPress does. For example,
with the excerpt_length filter you can change your Post Excerpt length (from default of 55 words).
 Enable WordPress features such as add_theme_support() to turn on Post Thumbnails, Post Formats, and Navigation Menus.
 Define functions you wish to re-use in multiple theme template files.
Beware: if a WordPress Plugin calls the same function, or filter, as you do in your functions file, the results can be unexpected -- even site-
disabling.

Search the web for "WordPress functions.php" to find suggestions to enhance the functionality of your WordPress site.

Shree U.P.E.T. Mahila BCA College – Dhrol | Lec. Mr. Rakesh Kanani 47

You might also like