Posted by The Doctor on Nov 30, 2011 with
No Comments |
Short Link
Last modified by The Doctor on April 26, 2012
in
Tips
as
add_filter,
get_bloginfo,
how-to,
is_frontpage,
is_home,
WordPress,
wp_title
A recent recommendation, starting with WordPress 3.3, from the WordPress Theme Review Team reads:
Themes are REQUIRED to use wp_title filter to filter wp_title() (RECOMMENDED), or pass argument to wp_title() (OPTIONALLY), in order to modify document title content
This has been discussed, worked through, and for the most part sorted out … but there doesn’t appear to be any sort of example code to work from. This presented an opportunity to have a look around the WordPress codex and the source code for some ideas how to filter the `wp_title` function.
Essentially, the idea behind the code (below) is to take a basic function call of `wp_title` add some relevant text (a la default themes: Twenty Ten / Twenty Eleven) then filter it back into the function; and finally to echo this to the HTML <title> tag.
To see the WPFA WP Title code click here.To hide the WPFA WP Title code click here.
<?php
if ( ! function_exists( 'wpfa_wp_title' ) ) {
/**
* WPFA WP Title
*
* Utilizes the `wp_title` filter to add text to the default output
*
* @package WPFA_WP_Title
* @version 0.2 - last revised April 26, 2012
*
* @link http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_title
* @link https://gist.github.com/1410493
*
* @param string $old_title - default title text
* @param string $sep - separator character
* @param string $sep_location - left|right - separator placement in relationship to title
*
* @return string - new title text
*/
function wpfa_wp_title( $old_title, $sep, $sep_location ) {
global $page, $paged;
/** Set initial title text */
$wpfa_title_text = $old_title . get_bloginfo( 'name' );
/** Add wrapping spaces to separator character */
$sep = ' ' . $sep . ' ';
/** Add the blog description (tagline) for the home/front page */
$site_tagline = get_bloginfo( 'description', 'display' );
if ( $site_tagline && ( is_home() || is_front_page() ) )
$wpfa_title_text .= "$sep$site_tagline";
/** Add a page number if necessary */
if ( $paged >= 2 || $page >= 2 )
$wpfa_title_text .= $sep . sprintf( __( 'Page %s', 'wpfa-wp-title-textdomain' ), max( $paged, $page ) );
return $wpfa_title_text;
}
}
add_filter( 'wp_title', 'wpfa_wp_title', 10, 3 ); ?>
Hopefully this will be useful, or maybe it will not … but it will be found in the next release of Desk Mess Mirrored as that is the theme where I first wrote and tested it with.
Enjoy!
To see the note click here.To hide the note click here.
PS: Note the
github:gist link in the code sample … feel free to download it from there, as well as copying it from here. The Doctor.
To see the note click here.To hide the note click here.
Code example revised April 26, 2012 to more correctly use the `wp_title` filter. Also to note, this method now expects the theme to simply call
wp_title() in the ‘header.php’ template. The Doctor.
Posted by The Doctor on Nov 25, 2009 with
1 Comment |
Short Link
Last modified by The Doctor on March 17, 2010
in
Functions
as
bloginfo,
get_bloginfo,
WordPress,
wp_list_categories,
wp_list_pages,
wp_page_menu
Creating text links back to the home page, or front page, of a blog are done in many ways. Many themes use the blog Title as a link back to the home page. It is also very common for the menu or navigation bar to link to the home page. This article will cover some examples of links for going home.
Let’s start with an obvious example: a link from the blog title back to the home page.
<a href="http://wpfirstaid.com/">WP First Aid</a>
The above example is very, very basic … let’s dress it up a bit:
<h1 class="blog-title"><a href="http://wpfirstaid.com/" alt="Going Home: WP First Aid">WP First Aid</a></h1>
Now we have a title using the h1 element and a CSS class of blog-title with some alt text letting the reader know where the link will be going. This is better but how does this help you? Let’s make use of WordPress’ built in functions to make this example more generic and easily transferable.
<h1 class="blog-title"><a href="<?php bloginfo('url'); ?>" alt="Going Home: <?php bloginfo('name'); ?>"><?php bloginfo('name'); ?></a></h1>
This example makes use of the WordPress function bloginfo(). Here is a link to the WordPress codex for this function; and, here is a link to the get_bloginfo() function which provides additional details.
Our next example is adding a home page link to a navigation bar. Many navigation bars are generated with one of three WordPress functions: wp_list_pages(), wp_page_menu(), or wp_list_categories(). The wp_page_menu function uses a value that creates a home page link, for example:
<?php wp_page_menu('show_home=1'); ?>
The wp_page_menu function includes all the available parameters of the wp_list_pages function and also includes wrapping the output in an unordered list.
To add a home page link into a theme using the wp_list_pages function you may look at the following example for guidance:
<ul>
<li><a href="<?php bloginfo('url'); ?>" alt="Going Home: <?php bloginfo('name'); ?>"><?php bloginfo('name'); ?></a></li>
<?php wp_list_pages(); ?>
</ul>
Adding a home page link to a navigation menu created with the wp_list_categories function is very similar. Let’s take a quick look at it below.
<ul>
<li><a href="<?php bloginfo('url'); ?>" alt="Going Home: <?php bloginfo('name'); ?>"><?php bloginfo('name'); ?></a></li>
<?php wp_list_categories(); ?>
</ul>
The only difference in the above two examples are the function call. The links we added to the home page are almost identical to the the link example we used for the blog title.
Feel free to use any of the above examples in your own works. Of course, there could be additional CSS elements needed to work with your theme’s style but I will leave that as an exercise for you … or feel free to contact us with more complex code requirements.