Tuesday, January 3, 2023
HomeWordPress Developmentphp - I get Deadly error: Can't redeclare cat_count_span() (beforehand declared in...)...

php – I get Deadly error: Can’t redeclare cat_count_span() (beforehand declared in…) Operate in Customized Class Widget


I created customized class widget for my very own theme and once I put this widget in most important sidebar and footer widget for instance i get that error
Deadly error: Can’t redeclare cat_count_span() (beforehand declared in E:xampphtdocsrepowp-contentpluginsconsulty-corewidgetscategoriesclass-consulty-cats.php:157) in E:xampphtdocsrepowp-contentpluginsconsulty-corewidgetscategoriesclass-consulty-cats.php on line 157
I used this perform cat_count_span so as to add span tag to posts rely and that i get it from this hyperlink
Wrap Put up rely in a span

Right here is the code i used

<?php
/**
 * Widget API: WP_Widget_Categories class
 *
 * @package deal WordPress
 * @subpackage Widgets
 * @since 4.4.0
 */

/**
 * Core class used to implement a Classes widget.
 *
 * @since 2.8.0
 *
 * @see WP_Widget
 */
class Consulty_Widget_Categories extends WP_Widget {

    /**
     * Units up a brand new Classes widget occasion.
     *
     * @since 2.8.0
     */
    public perform __construct() {
        $widget_ops = array(
            'classname'                   => 'consulty_widget_categories',
            // 'classname'                   => 'widget_categories',
            'description'                 => __( 'An inventory or dropdown of classes By Consulty.' ),
            'customize_selective_refresh' => true,
            'show_instance_in_rest'       => true,
        );
        mother or father::__construct( 'consulty_categories', __( 'Consulty Classes' ), $widget_ops );
        // mother or father::__construct( 'classes', __( 'Classes' ), $widget_ops );
    }

    /**
     * Outputs the content material for the present Classes widget occasion.
     *
     * @since 2.8.0
     * @since 4.2.0 Creates a novel HTML ID for the `<choose>` component
     *              if a couple of occasion is displayed on the web page.
     *
     * @param array $args     Show arguments together with 'before_title', 'after_title',
     *                        'before_widget', and 'after_widget'.
     * @param array $occasion Settings for the present Classes widget occasion.
     */
    public perform widget( $args, $occasion ) {
        static $first_dropdown = true;

        $default_title = __( 'Classes' );
        $title         = ! empty( $occasion['title'] ) ? $occasion['title'] : $default_title;

        /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
        $title = apply_filters( 'widget_title', $title, $occasion, $this->id_base );

        $rely        = ! empty( $occasion['count'] ) ? '1' : '0';
        $hierarchical = ! empty( $occasion['hierarchical'] ) ? '1' : '0';
        $dropdown     = ! empty( $occasion['dropdown'] ) ? '1' : '0';

        // Customized Required Courses Earlier than Consulty Class Widget

        echo '<div class="sidebar-widget sidebar-categories">';
        echo '<div class="widget-title">';

        // echo $args['before_widget'];

        if ( $title ) {
            echo $args['before_title'] . $title . $args['after_title'];
        }

        echo '</div>';// Finish class="widget-title"

        $cat_args = array(
            'orderby'      => 'title',
            'show_count'   => $rely,
            'hierarchical' => $hierarchical,
        );

        if ( $dropdown ) {
                printf( '<kind motion="%s" technique="get">', esc_url( home_url() ) );
                $dropdown_id    = ( $first_dropdown ) ? 'cat' : "{$this->id_base}-dropdown-{$this->quantity}";
                $first_dropdown = false;

                echo '<label class="screen-reader-text" for="' . esc_attr( $dropdown_id ) . '">' . $title . '</label>';

                $cat_args['show_option_none'] = __( 'Choose Class' );
                $cat_args['id']               = $dropdown_id;

                /**
                 * Filters the arguments for the Classes widget drop-down.
                 *
                 * @since 2.8.0
                 * @since 4.9.0 Added the `$occasion` parameter.
                 *
                 * @see wp_dropdown_categories()
                 *
                 * @param array $cat_args An array of Classes widget drop-down arguments.
                 * @param array $occasion Array of settings for the present widget.
                 */
                wp_dropdown_categories( apply_filters( 'widget_categories_dropdown_args', $cat_args, $occasion ) );

                echo '</kind>';

                $type_attr = current_theme_supports( 'html5', 'script' ) ? '' : ' sort="textual content/javascript"';
                ?>

                    <script<?php echo $type_attr; ?>>
                        /*
                        <![CDATA[ */
                        (function() {
                            var dropdown = document.getElementById( "<?php echo esc_js( $dropdown_id ); ?>" );
                            function onCatChange() {
                                if ( dropdown.options[ dropdown.selectedIndex ].worth > 0 ) {
                                    dropdown.parentNode.submit();
                                }
                            }
                            dropdown.onchange = onCatChange;
                        })();
                        /* ]]> */
                    </script>

                    <?php
        } else {
                $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';

                /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */
                $format = apply_filters( 'navigation_widgets_format', $format );

                if ( 'html5' === $format ) {
                    // The title could also be filtered: Strip out HTML and ensure the aria-label isn't empty.
                    $title      = trim( strip_tags( $title ) );
                    $aria_label = $title ? $title : $default_title;
                    echo '<nav aria-label="' . esc_attr( $aria_label ) . '">';
                }
                ?>
                <div class="widget-content">
                    <ul class="categories-list clearfix">
                        <!-- <ul> Default Tag -->
                        <?php
                            $cat_args['title_li'] = '';

                            /**
                             * Filters the arguments for the Classes widget.
                             *
                             * @since 2.8.0
                             * @since 4.9.0 Added the `$occasion` parameter.
                             *
                             * @param array $cat_args An array of Classes widget choices.
                             * @param array $occasion Array of settings for the present widget.
                             */

                            /**
                             * put class counts inside span tag
                             * https://wordpress.stackexchange.com/questions/17324/wrap-post-count-in-a-span
                             */
                            add_filter('wp_list_categories', 'cat_count_span');
                            perform cat_count_span($hyperlinks) {
                                $hyperlinks = str_replace('</a> (', '<span>(', $hyperlinks);
                                $hyperlinks = str_replace(')', ')</span></a>', $hyperlinks);
                            return $hyperlinks;
                            }

                            // ======================================

                            wp_list_categories( apply_filters( 'widget_categories_args', $cat_args, $occasion ) );
                            ?>
                    </ul>
                </div><!-- /.widget-content -->
                
                <?php
                        if ( 'html5' === $format ) {
                            echo '</nav>';
                        }
            }

        // echo $args['after_widget'];

        echo '</div>';// Finish class="sidebar-widget sidebar-categories"

    }

    /**
     * Handles updating settings for the present Classes widget occasion.
     *
     * @since 2.8.0
     *
     * @param array $new_instance New settings for this occasion as enter by the consumer by way of
     *                            WP_Widget::kind().
     * @param array $old_instance Outdated settings for this occasion.
     * @return array Up to date settings to avoid wasting.
     */
    public perform replace( $new_instance, $old_instance ) {
        $occasion                 = $old_instance;
        $occasion['title']        = sanitize_text_field( $new_instance['title'] );
        $occasion['count']        = ! empty( $new_instance['count'] ) ? 1 : 0;
        $occasion['hierarchical'] = ! empty( $new_instance['hierarchical'] ) ? 1 : 0;
        $occasion['dropdown']     = ! empty( $new_instance['dropdown'] ) ? 1 : 0;

        return $occasion;
    }

    /**
     * Outputs the settings kind for the Classes widget.
     *
     * @since 2.8.0
     *
     * @param array $occasion Present settings.
     */
    public perform kind( $occasion ) {
        // Defaults.
        $occasion     = wp_parse_args( (array) $occasion, array( 'title' => '' ) );
        $rely        = isset( $occasion['count'] ) ? (bool) $occasion['count'] : false;
        $hierarchical = isset( $occasion['hierarchical'] ) ? (bool) $occasion['hierarchical'] : false;
        $dropdown     = isset( $occasion['dropdown'] ) ? (bool) $occasion['dropdown'] : false;
        ?>
        <p>
            <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
            <enter class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>"
                title="<?php echo $this->get_field_name( 'title' ); ?>" sort="textual content"
                worth="<?php echo esc_attr( $occasion['title'] ); ?>" />
        </p>

        <p>
            <enter sort="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'dropdown' ); ?>"
                title="<?php echo $this->get_field_name( 'dropdown' ); ?>" <?php checked( $dropdown ); ?> />
            <label for="<?php echo $this->get_field_id( 'dropdown' ); ?>"><?php _e( 'Show as dropdown' ); ?></label>
            <br />

            <enter sort="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'rely' ); ?>"
                title="<?php echo $this->get_field_name( 'rely' ); ?>" <?php checked( $rely ); ?> />
            <label for="<?php echo $this->get_field_id( 'rely' ); ?>"><?php _e( 'Present put up counts' ); ?></label>
            <br />

            <enter sort="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'hierarchical' ); ?>"
                title="<?php echo $this->get_field_name( 'hierarchical' ); ?>" <?php checked( $hierarchical ); ?> />
            <label for="<?php echo $this->get_field_id( 'hierarchical' ); ?>"><?php _e( 'Present hierarchy' ); ?></label>
        </p>
        <?php
    }

}

// Register and cargo the widget
perform consulty_register_custom_cat() {
    register_widget( 'Consulty_Widget_Categories' );
}
add_action( 'widgets_init', 'consulty_register_custom_cat' );

I hope to seek out the answer right here

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments