Yetixx
Yetixx
Server: nginx/1.28.0
System: Linux instance-rr9enuui 6.1.0-15-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.66-1 (2023-12-09) x86_64
User: www (1000)
PHP: 8.0.26
Disabled: passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
Upload Files
File: /www/wwwroot/www.byte123.top/wp-content/themes/sUx/sidebar.php
<?php
/**
 * 侧边栏模板 (sidebar.php)
 *
 * 这个文件包含了网站侧边栏的内容,通常显示:
 * - 搜索框
 * - 分类列表
 * - 最近文章
 * - 标签云
 * - 其他小工具
 *
 * @package sUx
 */

// 防止直接访问文件
if (!defined('ABSPATH')) {
    exit;
}

/**
 * 检查当前页面是否应该显示侧边栏
 * 可以通过WordPress自定义器或页面设置来控制
 */
$show_sidebar = true;

// 可以根据页面类型决定是否显示侧边栏
if (is_page_template('page-full-width.php') || is_404()) {
    $show_sidebar = false;
}

// 也可以通过自定义字段控制
if (is_page() && get_post_meta(get_the_ID(), '_sux_hide_sidebar', true)) {
    $show_sidebar = false;
}

if ($show_sidebar) : ?>
    <aside id="sidebar" class="sidebar" role="complementary">
        <?php if (is_active_sidebar('sidebar-1')) : ?>

            <!-- 显示用户在WordPress后台设置的小工具 -->
            <?php dynamic_sidebar('sidebar-1'); ?>

        <?php else : ?>

            <!-- 如果没有设置小工具,显示默认的侧边栏内容 -->
            <div class="widget default-widget">
                <h3 class="widget-title"><?php _e('搜索网站', 'sux'); ?></h3>
                <?php get_search_form(); ?>
            </div>

            <div class="widget default-widget">
                <h3 class="widget-title"><?php _e('文章分类', 'sux'); ?></h3>
                <ul>
                    <?php
                    // 显示分类列表
                    wp_list_categories(array(
                        'title_li'    => '', // 不显示标题
                        'show_count'   => true, // 显示文章数量
                        'orderby'      => 'name', // 按名称排序
                        'order'        => 'ASC', // 升序排列
                    ));
                    ?>
                </ul>
            </div>

            <div class="widget default-widget">
                <h3 class="widget-title"><?php _e('最新文章', 'sux'); ?></h3>
                <ul>
                    <?php
                    // 显示最新的5篇文章
                    $recent_posts = wp_get_recent_posts(array(
                        'numberposts' => 5,
                        'post_status' => 'publish',
                        'post_type'   => 'post',
                    ));

                    foreach ($recent_posts as $post) :
                        setup_postdata($post);
                        ?>
                        <li>
                            <a href="<?php echo esc_url(get_permalink($post['ID'])); ?>">
                                <?php echo esc_html($post['post_title']); ?>
                            </a>
                            <small class="post-date">
                                <?php echo get_the_date('Y-m-d', $post['ID']); ?>
                            </small>
                        </li>
                    <?php endforeach;
                    wp_reset_postdata();
                    ?>
                </ul>
            </div>

            <div class="widget default-widget">
                <h3 class="widget-title"><?php _e('标签云', 'sux'); ?></h3>
                <div class="tag-cloud">
                    <?php
                    // 显示标签云
                    wp_tag_cloud(array(
                        'smallest' => 12, // 最小字体大小
                        'largest'  => 18, // 最大字体大小
                        'unit'     => 'px', // 字体单位
                        'number'   => 20, // 显示标签数量
                    ));
                    ?>
                </div>
            </div>

            <!-- 存档链接 -->
            <div class="widget default-widget">
                <h3 class="widget-title"><?php _e('文章存档', 'sux'); ?></h3>
                <ul>
                    <?php
                    // 显示月度存档
                    wp_get_archives(array(
                        'type'     => 'monthly',
                        'limit'     => 12, // 显示最近12个月
                        'show_post_count' => true,
                    ));
                    ?>
                </ul>
            </div>

            <?php if (is_single()) : ?>
                <!-- 仅在文章页面显示相关文章 -->
                <div class="widget default-widget">
                    <h3 class="widget-title"><?php _e('相关文章', 'sux'); ?></h3>
                    <ul>
                        <?php
                        // 获取当前文章的分类
                        $categories = get_the_category();
                        if ($categories) {
                            $category_id = $categories[0]->term_id;

                            // 查询相同分类的其他文章
                            $related_posts = new WP_Query(array(
                                'category__in'        => array($category_id),
                                'post__not_in'        => array(get_the_ID()),
                                'posts_per_page'      => 5,
                                'ignore_sticky_posts' => 1,
                            ));

                            if ($related_posts->have_posts()) :
                                while ($related_posts->have_posts()) :
                                    $related_posts->the_post();
                                    ?>
                                    <li>
                                        <a href="<?php the_permalink(); ?>">
                                            <?php the_title(); ?>
                                        </a>
                                    </li>
                                    <?php
                                endwhile;
                                wp_reset_postdata();
                            else :
                                ?>
                                <li><?php _e('暂无相关文章', 'sux'); ?></li>
                                <?php
                            endif;
                        }
                        ?>
                    </ul>
                </div>
            <?php endif; ?>

        <?php endif; ?>
    </aside>
<?php endif; ?>