1)wp_head()
DescriptionFire the 'wp_head' action. Put this template tag immediately before </head> tag in a theme template (ex. header.php, index.php).
Usage: <?php wp_head(); ?>
Parameters: This
function does not accept any parameters.Return values: None.
Examples
<?php
...
/* Always have wp_head() just before the closing </head>
* tag of your theme, or you will break many plugins, which
* generally use this hook to add elements to <head> such
* as styles, scripts, and meta tags.
*/
wp_head();
?>
</head>
2) get_footer()
Description
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
Default:
None
Return Values None.
Examples Simple 404 page
The following code is a simple example of a template for an "HTTP 404: Not Found" error (which you could include in your Theme as 404.php).<?php get_header(); ?>
<h2>Error 404 - Not Found</h2>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Multiple Footers
Different footer for different pages.<?php
if ( is_home() ) :
get_footer( 'home' );
elseif ( is_404() ) :
get_footer( '404' );
else :
get_footer();
endif;
?>
3) get_header()
Description
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
Default:
None
Examples Simple 404 page
The following code is a simple example of a template for an "HTTP 404: Not Found" error (which you could include in your Theme as 404.php).<?php get_header(); ?>
<h2>Error 404 - Not Found</h2>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Multiple Headers
Different header for different pages.<?php
if ( is_home() ) :
get_header( 'home' );
elseif ( is_404() ) :
get_header( '404' );
else :
get_header();
endif;
?>
4) get_sidebar()
Description
This function calls each of the active widget callbacks in order, which prints the markup for the sidebar. If you have more than one sidebar, you should give this function the name or number of the sidebar you want to print. This function returns true on success and false on failure.The return value should be used to determine whether to display a static sidebar. This ensures that your theme will look good even when the Widgets plug-in is not active.
If your sidebars were registered by number, they should be retrieved by number. If they had names when you registered them, use their names to retrieve them.
Usage
<?php dynamic_sidebar( $index ); ?>
Parameters index
(integer/string)
(optional) Name or ID of
dynamic sidebar.
Default:
1
Return Value(boolean)
True,
if widget sidebar was found and called. False if not found or not called.
Examples
Here is the recommended use of this function:<?php if ( is_active_sidebar( 'left-sidebar' ) ) : ?>
<ul id="sidebar">
<?php dynamic_sidebar( 'left-sidebar' ); ?>
</ul>
<?php endif; ?>
<ul id="sidebar">
<?php dynamic_sidebar( 'right-sidebar' ); ?>
</ul>
<ul id="sidebar">
<?php if ( ! dynamic_sidebar() ) : ?>
<li>{static sidebar item 1}</li>
<li>{static sidebar item 2}</li>
<?php endif; ?>
</ul>
5)get_search_form()
Description
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
(string|void) String when $echo is false.6)bloginfo()
Description
Retrieves information about the current blog. The function is located in: /wp-includes/general-template.php
Parameters
$show
(string)
(optional) What information to
get.
Default:
name
$filter
(string)
(optional) Get raw or display
information.
Default:
raw
$show
·
url
·
wpurl
·
description
·
rdf_url
·
rss_url
·
rss2_url
·
atom_url
·
comments_atom_url
·
comments_rss2_url
·
pingback_url
·
stylesheet_url
·
stylesheet_directory
·
template_directory
·
template_url
·
admin_email
·
charset
·
html_type
·
version
·
language
·
text_direction
·
name
Return Values
(string)
Returns
the requested information.
Usage
<?php get_bloginfo($show, $filter); ?>
7)wp_title()
Description
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: '»'
$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.
8)single_post_title()
Description
Displays or returns the title of the post when on a single post page (permalink page). This tag can be useful for displaying post titles outside The Loop.
Usage
<?php single_post_title( $prefix, $display ); ?>
Default Usage
<?php single_post_title(); ?>
Parameters
$prefix
(string)
(optional) Text to place before
the title.
Default:
None
$display
Default:
TRUE
Example
<h2><?php single_post_title( 'Current post: ' ); ?></h2>
9)wp_footer()
Description
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(); ?>
Parameters
This function does not accept any parameters.
Examples
In twentyten theme
wp-content/themes/twentyten/footer.php:...
<?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>
10)comments_template()
Description
The comments_template filter hook filters the path to the theme template file used for the comments template. It is part of the comments_template() function.The comments_template filter can be used to load a custom template form a plugin which replaces the theme's default comment template.
Parameters
$theme_template
(string)
(required) The path to the
theme template file.
Default:
None
Examples
A plugin can register as a content filter with the code:<?php add_filter( "comments_template", "my_plugin_comment_template" ); ?>
Where my_plugin_comment_template is the function WordPress should call when the comment_template() function is called on the theme. Note that the filter function the plugin defines must return the a full path to a template file or the resulting page will be blank.
This is an example of loading a different comments template for a custom post type:
<?php
function my_plugin_comment_template( $comment_template ) {
global $post;
if ( !( is_singular() && ( have_comments() || 'open' == $post->comment_status ) ) ) {
return;
}
if($post->post_type == 'business'){ // assuming there is a post type called business
return dirname(__FILE__) . '/reviews.php';
}
}
add_filter( "comments_template", "my_plugin_comment_template" );
?>
11)add_theme_support()
Description
Allows a theme or plugin to get support of a certain theme feature.
Usage
<?php get_theme_support( $feature ); ?>
Parameters
$feature
(string)
(required) Name for the feature
being added.
Default:
None
Features list:- Sidebar Widgets
- Navigation Menus
- Post Formats
- Post
Thumbnails
- Custom Backgrounds
- Custom
Headers
- Automatic Feed Links
- Editor Style
array()
- containing the arguments passed when support for the feature was registered viaadd_theme_support( $feature, $arguments )
; ORtrue
(bool) - if support for the feature was added without arguments (add_theme_support( $feature )
; ORfalse
(bool) - if the feature is not supported by the current theme.
12)get_template_directory_uri()
Description
Retrieves the absolute path to the directory of the current theme.Note: Does not contain a trailing slash.
Returns an absolute server path (eg: /home/user/public_html/wp-content/themes/my_theme), not a URI.
In the case a child theme is being used, the absolute path to the parent theme directory will be returned. Use get_stylesheet_directory() to get the absolute path to the child theme directory.
To retrieve the URI of the stylesheet directory use get_stylesheet_directory_uri() instead.
Usage
<?php echo get_template_directory(); ?>
Parameters
This tag has no parameters.
Return Values
(string)
Absolute
path to the directory of the current theme (without the trailing slash).
Examples
Include a PHP file<?php include( get_template_directory() . '/includes/myfile.php'); ?>
13)body_class()
Description
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.
Examples
Classes in WordPress Multisite
This could be used to provide custom classes for applying different styles to specific sites in Multisite that all use the same theme:
// 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;
}
No comments:
Post a Comment