星期三 , 22 1 月 2025

gpt写的自定义小工具代码


<?php // Register custom widget
function register_custom_widget() {
    register_widget('Custom_Content_Widget');
}
add_action('widgets_init', 'register_custom_widget');

// Create custom widget class
class Custom_Content_Widget extends WP_Widget {

    public function __construct() {
        parent::__construct(
            'custom_content_widget',
            __('Custom Content Widget', 'text_domain'),
            array(
                'description' => __('Display pages or custom taxonomy terms based on context', 'text_domain'),
            )
        );
    }

    // Widget frontend display
    public function widget($args, $instance) {
        echo $args['before_widget'];
        if (!empty($instance['title'])) {
            echo $args['before_title'] . apply_filters('widget_title', $instance['title']) . $args['after_title'];
        }

        if (is_singular('post')) { // Display pages for default post type
            $pages = get_pages();

            if ($pages) {
                echo '<div class="sider01"><div class="t1 d-none d-lg-block">' . apply_filters('widget_title', $instance['title']) . '</div><div class="d-flex position-relative"><div class="list type">';

                foreach ($pages as $page) {
                    echo '<a href="' . get_page_link($page->ID) . '" class="t2">' . esc_html($page->post_title) . '</a>';
                }

                echo '</div></div></div>';
            }
        } elseif (is_singular('product') || is_post_type_archive('product')) { // Display custom taxonomy terms for product post type
            $terms = get_terms(array(
                'taxonomy' => 'genre', // Replace 'genre' with your custom taxonomy slug
                'hide_empty' => false, // Set to true if you only want to display non-empty terms
            ));

            if ($terms && !is_wp_error($terms)) {
                echo '<div class="sider01"><div class="t1 d-none d-lg-block">' . apply_filters('widget_title', $instance['title']) . '</div><div class="d-flex position-relative"><div class="list type">';

                foreach ($terms as $term) {
                    echo '<a href="' . get_term_link($term) . '" class="t2">' . esc_html($term->name) . '</a>';
                }

                echo '</div></div></div>';
            }
        } elseif (is_singular('page')) { // Display pages for singular page
            $pages = get_pages();

            if ($pages) {
                echo '<div class="sider01"><div class="t1 d-none d-lg-block">' . apply_filters('widget_title', $instance['title']) . '</div><div class="d-flex position-relative"><div class="list type">';

                foreach ($pages as $page) {
                    echo '<a href="' . get_page_link($page->ID) . '" class="t2">' . esc_html($page->post_title) . '</a>';
                }

                echo '</div></div></div>';
            }
        }

        echo $args['after_widget'];
    }

    // Widget backend form
    public function form($instance) {
        $title = !empty($instance['title']) ? $instance['title'] : '';
        ?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>">
        </p>
        <?php
    }

    // Widget update
    public function update($new_instance, $old_instance) {
        $instance = array();
        $instance['title'] = (!empty($new_instance['title'])) ? strip_tags($new_instance['title']) : '';
        return $instance;
    }
}