widget-logic只在指定页面显示小工具
请在functions.php加入以下代码,以本主题为例:
function classic_widget_logic_update_callback($instance, $new_instance) { if (isset($new_instance['classic_widget_logic'])) { $instance['classic_widget_logic'] = $new_instance['classic_widget_logic']; } return $instance; } function classic_widget_logic_in_widget_form($widget, $return, $instance) { $logic = isset($instance['classic_widget_logic']) ? $instance['classic_widget_logic'] : classic_widget_logic_by_id($widget->id); ?> <p> <label for="<?php echo $widget->get_field_id('classic_widget_logic'); ?>">条件判断</label> <textarea class="widefat" name="<?php echo $widget->get_field_name('classic_widget_logic'); ?>" id="<?php echo $widget->get_field_id('classic_widget_logic'); ?>"><?php echo esc_textarea($logic) ?></textarea> </p> <?php return; } // function classic_widget_logic_by_id($widget_id) { global $classic_options; if (preg_match('/^(.+)-(\d+)$/', $widget_id, $m)) { $widget_class = $m[1]; $widget_i = $m[2]; $info = get_option('widget_' . $widget_class); if (empty($info[$widget_i])) { return ''; } $info = $info[$widget_i]; } else { $info = (array) get_option('widget_' . $widget_id, array()); } if (isset($info['classic_widget_logic'])) { $logic = $info['classic_widget_logic']; } elseif (isset($wl_options[$widget_id])) { $logic = stripslashes($wl_options[$widget_id]); classic_widget_logic_save($widget_id, $logic); unset($wl_options[$widget_id]); update_option('classic_widget_logic', $wl_options); } else { $logic = ''; } return $logic; } // 将数据存到options表中 function classic_widget_logic_save($widget_id, $logic) { global $classic_options; if (preg_match('/^(.+)-(\d+)$/', $widget_id, $m)) { $widget_class = $m[1]; $widget_i = $m[2]; $info = get_option('widget_' . $widget_class); if (!is_array($info[$widget_i])) { $info[$widget_i] = array(); } $info[$widget_i]['classic_widget_logic'] = $logic; update_option('widget_' . $widget_class, $info); } else { $info = (array) get_option('widget_' . $widget_id, array()); $info['classic_widget_logic'] = $logic; update_option('widget_' . $widget_id, $info); } } // CALLED ON 'sidebars_widgets' FILTER function classic_widget_logic_filter_sidebars_widgets($sidebars_widgets) { global $classic_options, $wl_in_customizer; if ($wl_in_customizer) { return $sidebars_widgets; } if (!empty($classic_options['classic_widget_logic-options-wp_reset_query'])) { wp_reset_query(); } foreach ($sidebars_widgets as $widget_area => $widget_list) { if ($widget_area == 'wp_inactive_widgets' || empty($widget_list)) { continue; } foreach ($widget_list as $pos => $widget_id) { $logic = classic_widget_logic_by_id($widget_id); // string(11) "is_single()" if (!classic_widget_logic_check_logic($logic)) { unset($sidebars_widgets[$widget_area][$pos]); } } } return $sidebars_widgets; } function classic_widget_logic_check_logic($logic) { $logic = @trim((string) $logic); $logic = apply_filters('classic_widget_logic_eval_override', $logic); if (is_bool($logic)) { return $logic; } if ($logic === '') { return true; } if (stristr($logic, "return") === false) { $logic = "return ( $logic );"; } try { $show_widget = eval($logic); } catch (Error $e) { trigger_error($e->getMessage(), E_USER_WARNING); $show_widget = false; } restore_error_handler(); return $show_widget; } // if (is_admin()) { add_filter('in_widget_form', 'classic_widget_logic_in_widget_form', 10, 3); add_filter('widget_update_callback', 'classic_widget_logic_update_callback', 10, 4); } else { add_filter('sidebars_widgets', 'classic_widget_logic_filter_sidebars_widgets', 10); }
判断支持与(&&)或(||)非(!)
is_home() 只在home页
is_home() || is_single() 在home页和文章页
!is_single() 除home页的其他页面
is_category('wordpress-theme-dev') 只在在wordpress-theme-dev的分类显示