<?php
/**
 * Sidebar Template
 *
 * This template displays the sidebar with widgets.
 *
 * @package Peblog
 */
?>

<aside id="sidebar" class="sidebar">
	<?php if ( is_active_sidebar( 'peblog-sidebar' ) ) : ?>
		<?php dynamic_sidebar( 'peblog-sidebar' ); ?>
	<?php else : ?>
		<div class="sidebar-widget">
			<h3 class="widget-title"><?php esc_html_e( 'Search', 'peblog' ); ?></h3>
			<?php get_search_form(); ?>
		</div>
		
		<div class="sidebar-widget">
			<h3 class="widget-title"><?php esc_html_e( 'Recent Posts', 'peblog' ); ?></h3>
			<ul>
				<?php
				$recent_posts = wp_get_recent_posts(
					array(
						'numberposts' => 5,
						'post_status' => 'publish',
					)
				);
				foreach ( $recent_posts as $recent_post ) :
					?>
					<li>
						<a href="<?php echo esc_url( get_permalink( $recent_post['ID'] ) ); ?>">
							<?php echo esc_html( $recent_post['post_title'] ); ?>
						</a>
					</li>
					<?php
				endforeach;
				wp_reset_postdata();
				?>
			</ul>
		</div>
		
		<div class="sidebar-widget">
			<h3 class="widget-title"><?php esc_html_e( 'Categories', 'peblog' ); ?></h3>
			<ul>
				<?php wp_list_categories( array( 'title_li' => '' ) ); ?>
			</ul>
		</div>
	<?php endif; ?>
</aside>

