Php Wordpress类别小部件下拉列表

Php Wordpress类别小部件下拉列表,php,wordpress,widget,Php,Wordpress,Widget,如何强制Category小部件(下拉式)链接到Category slug而不是?cat=#?我在wordpress论坛上找到了这个。这是一篇很老的文章,但可以解决你的问题。 我们可以强制显示来覆盖我们要求的默认wordpress类别小部件。这不是覆盖默认wordpress小部件的最佳方法。这将是最好的注册新的小部件类和自定义方法。我认为您可以复制相同的小部件类更改名称、变量、id,使其与默认小部件相同,但不同,这不会影响默认wordpress小部件。下面的override wordpress

如何强制Category小部件(下拉式)链接到Category slug而不是?cat=#?

我在wordpress论坛上找到了这个。这是一篇很老的文章,但可以解决你的问题。


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

<?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
} 

/*  */

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


您的产品类别是什么?您是否将类别库更改为您的类别slug?请澄清你的问题!任何类别。每个人都有自己的鼻涕虫,我有成千上万的鼻涕虫。默认情况下,“Category Widget”显示通过各自的slug链接到类别的所有链接。当您将其更改为下拉列表而不是列表时,下拉列表中的所有类别都将更改为?cat=#而不是slug(/category/the_slug)。您是否更改了?是的,更改了几次。我有%postname%,因为我的一些页面需要它
<?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
}