Skip to content

Instantly share code, notes, and snippets.

@rfmeier
Last active June 14, 2017 15:10
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rfmeier/5718832 to your computer and use it in GitHub Desktop.
Save rfmeier/5718832 to your computer and use it in GitHub Desktop.
Make the Genesis Featured Post widget title link to the specified category archives.
<?php
add_filter( 'widget_title', 'custom_widget_title', 10, 3 );
/**
* Callback for WordPress 'widget_title' filter.
*
* Create a link for the title of the Genesis Featured Widget if a category is
* specified.
*
* @package WordPress
* @category Widget
* @author Ryan Meier http://www.rfmeier.net
*
* @param string $title The widget title
* @param object $instance The instance of the current widget
* @param string $id_base the base id
* @return string $title The widget title
*/
function custom_widget_title( $title, $instance=false, $id_base='' ){
// if not the featured image post, return title
if( 'featured-post' != $id_base )
return $title;
// if the instance does not specify a category, return title
if( empty( $instance['posts_cat'] ) )
return $title;
// get the category link from the id
$category_link = get_category_link( $instance['posts_cat'] );
// get the category name from the id
$category_name = get_the_category_by_ID( $instance['posts_cat'] );
// build the category link
$title_link = sprintf( '<a href="%s" title="%s">%s</a>',
esc_url( $category_link ),
esc_attr( $category_name ),
esc_html( $title ) );
// return the category link title
return $title_link;
}
@graphicdesignbyemily
Copy link

Perfect- thanks!!

@graphicdesignbyemily
Copy link

What if you are using a widget and you aren't specifying a category, and instead are using 'all categories' - anyway to make it link to the blog?

@steckinsights
Copy link

Thanks @rfmeier!

@genevishgraphics, I know your question is almost a year old, but perhaps it will help someone else. In my tests, setting the category to "uncategorized" still returns the uncategorized posts. I was guess that if you were not having the same results, you could remove lines 25-27 so the function would not check to see if the category was empty.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment