Wordpress类别小工具下拉条


Wordpress Category Widget Drop-Down Slugs

如何强制Category Widget(下拉样式)链接到类别段塞而不是?cat=#?

我们可以根据需要强制显示以覆盖默认的wordpress类别小部件。这并不是覆盖默认wordpress窗口小部件的最佳方法。这将是最好的注册新的小部件类和自定义方法。我认为您可以复制相同的小部件类更改名称、变量、id,使其与默认的小部件相同,但不同不会影响默认的wordpress小部件。下面的override wordpress默认类别小部件下拉更改显示为您的自定义,如果这对您有帮助的话。

<?php 
/*
 * Plugin Name: Test
*/

add_action( 'widgets_init', 'plugin_prefix_widgets_init' );
function plugin_prefix_widgets_init(){
    register_widget( 'WP_Widget_Custom_Categories' );
}
if( ! class_exists( 'WP_Widget_Categories' ) )
include ABSPATH.'wp-includes/widgets/class-wp-widget-categories.php';
class WP_Widget_Custom_Categories extends WP_Widget_Categories{

    public function widget( $args, $instance ) {
        // Apply conditinal tag to sepicific template only. Any other display to remove override widget display.
        /*if( ! is_page('page_slug') ){
          parent::widget( $args, $instance );
          return;   
        }*/

        static $first_dropdown = true;
        /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Categories' ) : $instance['title'], $instance, $this->id_base );
        $c = ! empty( $instance['count'] ) ? '1' : '0';
        $h = ! empty( $instance['hierarchical'] ) ? '1' : '0';
        $d = ! empty( $instance['dropdown'] ) ? '1' : '0';
        echo $args['before_widget'];
        if ( $title ) {
            echo $args['before_title'] . $title . $args['after_title'];
        }
        $cat_args = array(
            'orderby'      => 'name',
            'show_count'   => $c,
            'hierarchical' => $h
        );
        if ( $d ) {
            $dropdown_id = ( $first_dropdown ) ? 'cat' : "{$this->id_base}-dropdown-{$this->number}";
            $first_dropdown = false;
            echo '<label class="screen-reader-text" for="' . esc_attr( $dropdown_id ) . '">' . $title . '</label>';
            $cat_args['show_option_none'] = __( 'Select Category' );
            $cat_args['id'] = $dropdown_id;
            $cat_args['value_field'] = 'slug';
            if( is_category() )
            $cat_args['selected'] = get_term( get_query_var('cat'), 'category' )->slug ;
            /**
             * Filter the arguments for the Categories widget drop-down.
             *
             * @since 2.8.0
             *
             * @see wp_dropdown_categories()
             *
             * @param array $cat_args An array of Categories widget drop-down arguments.
             */
            wp_dropdown_categories( apply_filters( 'widget_categories_dropdown_args', $cat_args ) );
            ?>
<script type='text/javascript'>
/* <![CDATA[ */
(function() {
    var dropdown = document.getElementById( "<?php echo esc_js( $dropdown_id ); ?>" );
    function onCatChange() {
        if ( dropdown.options[ dropdown.selectedIndex ].value != -1 ) {
            location.href = "<?php echo home_url(); ?>/category/"+dropdown.options[ dropdown.selectedIndex ].value;
        }
    }
    dropdown.onchange = onCatChange;
})();
/* ]]> */
</script>
<?php
        } else {
?>
        <ul>
<?php
        $cat_args['title_li'] = '';
        /**
         * Filter the arguments for the Categories widget.
         *
         * @since 2.8.0
         *
         * @param array $cat_args An array of Categories widget options.
         */
        wp_list_categories( apply_filters( 'widget_categories_args', $cat_args ) );
?>
        </ul>
<?php
        }
        echo $args['after_widget'];
    }
    // Only need to change customize widget method to display as requirement
} 

抱歉英语不好。

我在wordpress论坛上找到了这个。这是一篇相当古老的帖子,但解决了你的问题。猫鼻涕虫在下拉wp论坛

<?php
function replace_id_for_slug($option){
$categories = get_categories("hide_empty=0");
preg_match('/value="('d*)"/', $option[0], $matches);
$id = $matches[1];
$slug = "";
foreach($categories as $category){
    if($category->cat_ID == $id){
        $slug = $category->slug;
    }
}
return preg_replace("/value='"('d*)'"/", "value='"$slug'"", $option[0]);
}
$select = wp_dropdown_categories("hierarchical=1&hide_empty=0&echo=0");
$select = preg_replace_callback("#<option[^>]*>[^<]*</option>#", "replace_id_for_slug", $select);
echo $select;
?>
<script type="text/javascript"><!--
var dropdown = document.getElementById("cat");
function onCatChange() {
    if ( dropdown.options[dropdown.selectedIndex].value != -1 ) {
        location.href = "<?php echo get_option('home');?>/"+dropdown.options[dropdown.selectedIndex].value+"/";
    }
}
dropdown.onchange = onCatChange;
--></script>

如果你不反对额外的插件,也有这个选项,因为其他答案是分解你的代码。

https://wordpress.org/plugins/extended-categories-widget/