Reputation: 1125
I have added a custom template for an image widget which is defined in functions.php:
add_filter('sp_template_image-widget_widget.php','my_template_filter');
function my_template_filter($template) {
return get_template_directory() . '/widget-templates/image-widget-custom.php';
}
The contents of this template (image-widget-custom.php) are shown here:
<?php
/**
* Widget template. This template can be overriden using the "sp_template_image-widget_widget.php" filter.
* See the readme.txt file for more info.
*/
// Block direct requests
if ( !defined('ABSPATH') )
die('-1');
echo '<div class="col-sm-3 footer-blocks">';
echo $before_widget;
$output = "";
$output .= '<a href="' . $instance[link] . '">';
$output .= '<div class="hover-wrapper">';
$output .= '<div class="hover-inner">';
$output .= '<div class="hover-border">';
$output .= '<h2 class="text-uppercase">' . $instance[title] . '</h2>';
$output .= '</div>';
$output .= '</div>';
$output .= '<img class="img-responsive" src="' . $instance[imageurl] . '" alt="Footer image">';
$output .= '</div>';
$output .= '</a>';
echo $output;
echo $after_widget;
echo '</div>';
?>
The only thing is, I want the widget to be formatted for a specific widget area, not everywhere it is used on the wordpress website. The area I want the template applied to is in the footer:
<?php dynamic_sidebar( 'footer-blocks' ); ?>
Is this possible?
Upvotes: 1
Views: 1462
Reputation: 26160
Yes, this is possible.
One way - assuming you can modify theme template files - is to add the filter just before the sidebar call, and then remove the filter just after the sidebar call.
Remove the add_filter
that you have in your theme files, and then modify your code like so:
<?php
// Add the filter just before the sidebar is called
add_filter('sp_template_image-widget_widget.php','my_template_filter');
// Call the sidebar, which will use your custom template
dynamic_sidebar( 'footer-blocks' );
// Remove the filter to ensure any other sidebars do not use custom template
remove_filter('sp_template_image-widget_widget.php','my_template_filter');
?>
Note that remove_filter will ensure that any OTHER calls to the widget will not be filtered with your custom template.
Alternate Method (preferred / better):
The more "WordPress Way" to do this would be to leverage the dynamic_sidebar_before
and dynamic_sidebar_after
functions, and in doing so you can check which sidebar is being loaded, and add the filter ONLY for the correct sidebar(s). This code would go in your theme's functions.php file:
add_action( 'dynamic_sidebar_before', 'my_sidebar_checker', 10, 2);
add_action( 'dynamic_sidebar_after', 'my_sidebar_checker_after', 10, 2);
function my_sidebar_checker( $index, $bool ) {
if ( 'footer-blocks' == $index ) {
// Add the filter just before the sidebar is called
add_filter('sp_template_image-widget_widget.php','my_template_filter');
}
}
function my_sidebar_checker_after( $index, $bool ) {
if ( 'footer-blocks' == $index ) {
// Remove the filter to ensure any other sidebars do not use custom template
remove_filter('sp_template_image-widget_widget.php','my_template_filter');
}
}
Upvotes: 1