Skip to content

Instantly share code, notes, and snippets.

@whatnickcodes
Created February 23, 2015 16:39
Show Gist options
  • Save whatnickcodes/372275faaacaa3e5c5a5 to your computer and use it in GitHub Desktop.
Save whatnickcodes/372275faaacaa3e5c5a5 to your computer and use it in GitHub Desktop.
WordPress better Get Title
<?php
/* Add to Functions.php */
function grab_title() {
if (is_page() || is_single())
{
return get_the_title();
}
elseif (is_archive() && !is_category() && !is_tag() && !is_author() && !is_tax())
{
if (is_year()) return get_the_time('Y');
if (is_month()) return get_the_time('F, Y');
if (is_day()) return get_the_time('F j, Y');
}
elseif (is_tag())
{
return single_tag_title('', FALSE);
}
elseif (is_search() && $_GET['s'] != '')
{
$search = get_search_query();
if (strlen($search) > 40) $search = substr($search, 0, 35).'...';
return $search;
}
elseif (is_search() && $_GET['s'] == '')
{
return 'All Posts';
}
elseif (is_404())
{
return '404';
}
elseif (is_author())
{
$author = get_user_by('id', $post->post_author)->data;
return get_the_author_meta('first_name', $author->ID).' '.get_the_author_meta('last_name', $author->ID);
}
elseif (is_category())
{
$category = get_category(get_query_var('cat'));
return $category->name;
}
elseif (is_tax())
{
return $wp_query->queried_object->name;
}
else
{
return FALSE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment