Wordpress类别小部件下拉式小块

时间:2016-04-15 18:59:12

标签: php wordpress widget

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

3 个答案:

答案 0 :(得分:2)

我在wordpress论坛上发现了这个。它很老的帖子,但解决了你的问题。 cat slug in dropdown wp forums

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

答案 1 :(得分:2)

我们可以强制显示覆盖我们要求的默认wordpress类别小部件。这不是覆盖默认wordpress小部件的最佳方法。最好注册新的widget类和自定义方法。我认为您可以复制相同的窗口小部件类更改名称,变量,ID以使其与默认窗口小部件相同但不同它不会影响默认的wordpress窗口小部件。如果这对您有所帮助,则覆盖下面的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
} 

抱歉英语不好。

答案 2 :(得分:2)

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

http://www.public.iastate.edu/~mervyn/stat580/Notes/s09rn.pdf