【问题标题】:Wordpress add_filter condition for widget area小部件区域的 Wordpress add_filter 条件
【发布时间】:2016-12-13 18:03:01
【问题描述】:

我为在functions.php 中定义的image widget 添加了一个自定义模板:

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';
}

此模板(image-widget-custom.php)的内容如下所示:

<?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>';
?>

唯一的事情是,我希望小部件针对特定的小部件区域进行格式化,而不是在 wordpress 网站上使用的任何地方。我想要应用模板的区域在页脚中:

<?php dynamic_sidebar( 'footer-blocks' ); ?> 

这可能吗?

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    是的,这是可能的。

    一种方法 - 假设您可以修改主题模板文件 - 是在侧边栏调用之前添加过滤器,然后在侧边栏调用之后删除过滤器

    删除您在主题文件中的add_filter,然后像这样修改您的代码:

     <?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');
     ?> 
    

    请注意,remove_filter 将确保对小部件的任何其他调用都不会被您的自定义模板过滤。

    替代方法(首选/更好):

    更多的“WordPress 方式”是利用dynamic_sidebar_beforedynamic_sidebar_after 函数,这样做您可以检查正在加载哪个侧边栏,并添加过滤器仅适用于正确的侧边栏。此代码将进入您主题的 functions.php 文件:

    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');
        }
    }
    

    【讨论】:

    • 非常感谢,我使用了您的替代方法,这对我很有效。
    猜你喜欢
    • 2023-04-01
    • 2015-07-17
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多