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