I’ll build a strong sidebar.php for your RX theme with widget areas, sticky TOC, search, author card, categories, recent/related posts, popular placeholders, newsletter box, ads slots, breadcrumbs-safe logic, and fallback content when no widgets are active.
My dear friend, create this file:
rx-theme/sidebar.php
<?php
/**
* Sidebar Template
*
* Advanced RX Theme sidebar with:
* - Dynamic widget areas
* - Search
* - Author box
* - Table of contents placeholder
* - Recent posts
* - Categories
* - Tags
* - Popular posts placeholder
* - Related posts
* - Sticky sidebar support
* - Ad zones
* - Newsletter box
* - Medical disclaimer box
* - Mobile-safe structure
*
* @package RX_Theme
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Prevent sidebar on selected templates.
*/
if (
is_page_template( 'templates/full-width.php' ) ||
is_page_template( 'templates/landing-page.php' ) ||
is_attachment()
) {
return;
}
$rx_sidebar_classes = array(
'rx-sidebar',
'widget-area',
'rx-site-sidebar',
);
if ( is_single() ) {
$rx_sidebar_classes[] = 'rx-sidebar-single';
}
if ( is_page() ) {
$rx_sidebar_classes[] = 'rx-sidebar-page';
}
if ( is_home() || is_archive() || is_search() ) {
$rx_sidebar_classes[] = 'rx-sidebar-archive';
}
if ( is_active_sidebar( 'sidebar-1' ) ) {
$rx_sidebar_classes[] = 'rx-sidebar-has-widgets';
} else {
$rx_sidebar_classes[] = 'rx-sidebar-fallback';
}
$rx_sidebar_classes = apply_filters( 'rx_sidebar_classes', $rx_sidebar_classes );
?>
<aside id="secondary" class="<?php echo esc_attr( implode( ' ', $rx_sidebar_classes ) ); ?>" role="complementary" aria-label="<?php esc_attr_e( 'Main Sidebar', 'rx-theme' ); ?>">
<?php do_action( 'rx_before_sidebar' ); ?>
<div class="rx-sidebar-inner">
<?php do_action( 'rx_sidebar_top' ); ?>
<?php
/**
* Main registered WordPress sidebar.
*
* Register this in functions.php:
*
* register_sidebar( array(
* 'name' => esc_html__( 'Main Sidebar', 'rx-theme' ),
* 'id' => 'sidebar-1',
* 'description' => esc_html__( 'Add widgets here.', 'rx-theme' ),
* 'before_widget' => '<section id="%1$s" class="widget %2$s rx-widget">',
* 'after_widget' => '</section>',
* 'before_title' => '<h2 class="widget-title rx-widget-title">',
* 'after_title' => '</h2>',
* ) );
*/
?>
<?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>
<div class="rx-sidebar-widgets rx-sidebar-dynamic-widgets">
<?php dynamic_sidebar( 'sidebar-1' ); ?>
</div>
<?php else : ?>
<div class="rx-sidebar-default-widgets">
<section class="widget rx-widget rx-widget-search">
<h2 class="widget-title rx-widget-title">
<?php esc_html_e( 'Search', 'rx-theme' ); ?>
</h2>
<?php get_search_form(); ?>
</section>
<?php if ( is_single() && get_the_author_meta( 'description' ) ) : ?>
<section class="widget rx-widget rx-widget-author">
<h2 class="widget-title rx-widget-title">
<?php esc_html_e( 'About the Author', 'rx-theme' ); ?>
</h2>
<div class="rx-author-card">
<div class="rx-author-avatar">
<?php echo get_avatar( get_the_author_meta( 'ID' ), 80 ); ?>
</div>
<div class="rx-author-content">
<h3 class="rx-author-name">
<?php echo esc_html( get_the_author() ); ?>
</h3>
<p class="rx-author-description">
<?php echo esc_html( get_the_author_meta( 'description' ) ); ?>
</p>
<a class="rx-author-link" href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>">
<?php esc_html_e( 'View all posts', 'rx-theme' ); ?>
</a>
</div>
</div>
</section>
<?php endif; ?>
<?php if ( is_single() ) : ?>
<section class="widget rx-widget rx-widget-toc">
<h2 class="widget-title rx-widget-title">
<?php esc_html_e( 'Article Guide', 'rx-theme' ); ?>
</h2>
<nav class="rx-table-of-contents" aria-label="<?php esc_attr_e( 'Article table of contents', 'rx-theme' ); ?>">
<p>
<?php esc_html_e( 'Use the headings inside this article to quickly understand the main sections.', 'rx-theme' ); ?>
</p>
<ul class="rx-toc-placeholder">
<li>
<a href="#content">
<?php esc_html_e( 'Start reading', 'rx-theme' ); ?>
</a>
</li>
<li>
<a href="#comments">
<?php esc_html_e( 'Discussion', 'rx-theme' ); ?>
</a>
</li>
</ul>
</nav>
</section>
<?php endif; ?>
<section class="widget rx-widget rx-widget-recent-posts">
<h2 class="widget-title rx-widget-title">
<?php esc_html_e( 'Recent Posts', 'rx-theme' ); ?>
</h2>
<?php
$rx_recent_posts = new WP_Query(
array(
'post_type' => 'post',
'posts_per_page' => 5,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
'no_found_rows' => true,
)
);
?>
<?php if ( $rx_recent_posts->have_posts() ) : ?>
<ul class="rx-sidebar-post-list rx-recent-post-list">
<?php
while ( $rx_recent_posts->have_posts() ) :
$rx_recent_posts->the_post();
?>
<li class="rx-sidebar-post-item">
<a href="<?php the_permalink(); ?>" class="rx-sidebar-post-link">
<?php if ( has_post_thumbnail() ) : ?>
<span class="rx-sidebar-post-thumb">
<?php
the_post_thumbnail(
'thumbnail',
array(
'loading' => 'lazy',
'alt' => the_title_attribute(
array(
'echo' => false,
)
),
)
);
?>
</span>
<?php endif; ?>
<span class="rx-sidebar-post-text">
<span class="rx-sidebar-post-title">
<?php the_title(); ?>
</span>
<span class="rx-sidebar-post-date">
<?php echo esc_html( get_the_date() ); ?>
</span>
</span>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php esc_html_e( 'No recent posts found.', 'rx-theme' ); ?></p>
<?php endif; ?>
</section>
<?php
$rx_categories = get_categories(
array(
'orderby' => 'count',
'order' => 'DESC',
'number' => 10,
'hide_empty' => true,
)
);
?>
<?php if ( ! empty( $rx_categories ) && ! is_wp_error( $rx_categories ) ) : ?>
<section class="widget rx-widget rx-widget-categories">
<h2 class="widget-title rx-widget-title">
<?php esc_html_e( 'Top Categories', 'rx-theme' ); ?>
</h2>
<ul class="rx-category-list">
<?php foreach ( $rx_categories as $rx_category ) : ?>
<li class="rx-category-item">
<a href="<?php echo esc_url( get_category_link( $rx_category->term_id ) ); ?>">
<span class="rx-category-name">
<?php echo esc_html( $rx_category->name ); ?>
</span>
<span class="rx-category-count">
<?php echo esc_html( number_format_i18n( $rx_category->count ) ); ?>
</span>
</a>
</li>
<?php endforeach; ?>
</ul>
</section>
<?php endif; ?>
<?php
$rx_tags = get_tags(
array(
'orderby' => 'count',
'order' => 'DESC',
'number' => 20,
'hide_empty' => true,
)
);
?>
<?php if ( ! empty( $rx_tags ) && ! is_wp_error( $rx_tags ) ) : ?>
<section class="widget rx-widget rx-widget-tags">
<h2 class="widget-title rx-widget-title">
<?php esc_html_e( 'Popular Topics', 'rx-theme' ); ?>
</h2>
<div class="rx-tag-cloud">
<?php foreach ( $rx_tags as $rx_tag ) : ?>
<a class="rx-tag-link" href="<?php echo esc_url( get_tag_link( $rx_tag->term_id ) ); ?>">
<?php echo esc_html( $rx_tag->name ); ?>
</a>
<?php endforeach; ?>
</div>
</section>
<?php endif; ?>
</div>
<?php endif; ?>
<?php if ( is_active_sidebar( 'sidebar-sticky' ) ) : ?>
<div class="rx-sidebar-sticky-zone">
<?php dynamic_sidebar( 'sidebar-sticky' ); ?>
</div>
<?php else : ?>
<div class="rx-sidebar-sticky-zone">
<?php if ( is_single() ) : ?>
<section class="widget rx-widget rx-widget-related">
<h2 class="widget-title rx-widget-title">
<?php esc_html_e( 'Related Articles', 'rx-theme' ); ?>
</h2>
<?php
$rx_current_post_id = get_the_ID();
$rx_category_ids = wp_get_post_categories( $rx_current_post_id );
$rx_related_args = array(
'post_type' => 'post',
'posts_per_page' => 4,
'post_status' => 'publish',
'post__not_in' => array( $rx_current_post_id ),
'ignore_sticky_posts' => true,
'no_found_rows' => true,
);
if ( ! empty( $rx_category_ids ) ) {
$rx_related_args['category__in'] = $rx_category_ids;
}
$rx_related_posts = new WP_Query( $rx_related_args );
?>
<?php if ( $rx_related_posts->have_posts() ) : ?>
<ul class="rx-sidebar-post-list rx-related-post-list">
<?php
while ( $rx_related_posts->have_posts() ) :
$rx_related_posts->the_post();
?>
<li class="rx-sidebar-post-item">
<a href="<?php the_permalink(); ?>" class="rx-sidebar-post-link">
<?php if ( has_post_thumbnail() ) : ?>
<span class="rx-sidebar-post-thumb">
<?php
the_post_thumbnail(
'thumbnail',
array(
'loading' => 'lazy',
'alt' => the_title_attribute(
array(
'echo' => false,
)
),
)
);
?>
</span>
<?php endif; ?>
<span class="rx-sidebar-post-text">
<span class="rx-sidebar-post-title">
<?php the_title(); ?>
</span>
<span class="rx-sidebar-post-date">
<?php echo esc_html( get_the_date() ); ?>
</span>
</span>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php esc_html_e( 'No related articles found.', 'rx-theme' ); ?></p>
<?php endif; ?>
</section>
<?php endif; ?>
<section class="widget rx-widget rx-widget-newsletter">
<h2 class="widget-title rx-widget-title">
<?php esc_html_e( 'Stay Updated', 'rx-theme' ); ?>
</h2>
<p>
<?php esc_html_e( 'Get new health, medical, and educational articles directly from RX Theme updates.', 'rx-theme' ); ?>
</p>
<form class="rx-newsletter-form" method="post" action="#">
<label class="screen-reader-text" for="rx-sidebar-newsletter-email">
<?php esc_html_e( 'Email address', 'rx-theme' ); ?>
</label>
<input
type="email"
id="rx-sidebar-newsletter-email"
name="rx_newsletter_email"
placeholder="<?php esc_attr_e( 'Your email address', 'rx-theme' ); ?>"
required
/>
<button type="submit" class="rx-newsletter-submit">
<?php esc_html_e( 'Subscribe', 'rx-theme' ); ?>
</button>
</form>
</section>
<section class="widget rx-widget rx-widget-medical-disclaimer">
<h2 class="widget-title rx-widget-title">
<?php esc_html_e( 'Medical Disclaimer', 'rx-theme' ); ?>
</h2>
<p>
<?php esc_html_e( 'This website provides educational information only. It should not replace professional medical advice, diagnosis, or treatment.', 'rx-theme' ); ?>
</p>
</section>
<section class="widget rx-widget rx-widget-ad rx-sidebar-ad">
<h2 class="screen-reader-text">
<?php esc_html_e( 'Advertisement', 'rx-theme' ); ?>
</h2>
<div class="rx-ad-box">
<?php
/**
* Add AdSense, affiliate banner, or custom advertisement here.
*
* Example:
* echo '<ins class="adsbygoogle"></ins>';
*/
do_action( 'rx_sidebar_ad_slot' );
?>
<?php if ( ! has_action( 'rx_sidebar_ad_slot' ) ) : ?>
<span><?php esc_html_e( 'Advertisement Area', 'rx-theme' ); ?></span>
<?php endif; ?>
</div>
</section>
</div>
<?php endif; ?>
<?php do_action( 'rx_sidebar_bottom' ); ?>
</div>
<?php do_action( 'rx_after_sidebar' ); ?>
</aside>
For this sidebar to work fully, add these sidebar registrations in your functions.php or inc/core/sidebars.php:
<?php
/**
* Register RX Theme sidebars.
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
function rx_theme_register_sidebars() {
register_sidebar(
array(
'name' => esc_html__( 'Main Sidebar', 'rx-theme' ),
'id' => 'sidebar-1',
'description' => esc_html__( 'Main sidebar area for posts, pages, archives, and blog pages.', 'rx-theme' ),
'before_widget' => '<section id="%1$s" class="widget %2$s rx-widget">',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title rx-widget-title">',
'after_title' => '</h2>',
)
);
register_sidebar(
array(
'name' => esc_html__( 'Sticky Sidebar', 'rx-theme' ),
'id' => 'sidebar-sticky',
'description' => esc_html__( 'Sticky sidebar area for related posts, ads, newsletter, or important notices.', 'rx-theme' ),
'before_widget' => '<section id="%1$s" class="widget %2$s rx-widget rx-sticky-widget">',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title rx-widget-title">',
'after_title' => '</h2>',
)
);
register_sidebar(
array(
'name' => esc_html__( 'Post Sidebar', 'rx-theme' ),
'id' => 'sidebar-post',
'description' => esc_html__( 'Optional sidebar area for single blog posts.', 'rx-theme' ),
'before_widget' => '<section id="%1$s" class="widget %2$s rx-widget rx-post-widget">',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title rx-widget-title">',
'after_title' => '</h2>',
)
);
register_sidebar(
array(
'name' => esc_html__( 'Page Sidebar', 'rx-theme' ),
'id' => 'sidebar-page',
'description' => esc_html__( 'Optional sidebar area for static pages.', 'rx-theme' ),
'before_widget' => '<section id="%1$s" class="widget %2$s rx-widget rx-page-widget">',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title rx-widget-title">',
'after_title' => '</h2>',
)
);
register_sidebar(
array(
'name' => esc_html__( 'Archive Sidebar', 'rx-theme' ),
'id' => 'sidebar-archive',
'description' => esc_html__( 'Optional sidebar area for category, tag, author, search, and archive pages.', 'rx-theme' ),
'before_widget' => '<section id="%1$s" class="widget %2$s rx-widget rx-archive-widget">',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title rx-widget-title">',
'after_title' => '</h2>',
)
);
register_sidebar(
array(
'name' => esc_html__( 'Sidebar Advertisement', 'rx-theme' ),
'id' => 'sidebar-ad',
'description' => esc_html__( 'Advertisement widget area for sidebar banners or AdSense code.', 'rx-theme' ),
'before_widget' => '<section id="%1$s" class="widget %2$s rx-widget rx-ad-widget">',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title rx-widget-title">',
'after_title' => '</h2>',
)
);
}
add_action( 'widgets_init', 'rx_theme_register_sidebars' );
And add this CSS in style.css for clean design:
.rx-sidebar {
width: 100%;
max-width: 360px;
}
.rx-sidebar-inner {
display: flex;
flex-direction: column;
gap: 24px;
}
.rx-widget {
background: #ffffff;
border: 1px solid rgba(0, 0, 0, 0.08);
border-radius: 14px;
padding: 20px;
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.04);
}
.rx-widget-title {
margin: 0 0 16px;
font-size: 20px;
line-height: 1.3;
font-weight: 700;
}
.rx-sidebar-post-list,
.rx-category-list {
list-style: none;
margin: 0;
padding: 0;
}
.rx-sidebar-post-item {
margin-bottom: 14px;
}
.rx-sidebar-post-link {
display: flex;
gap: 12px;
text-decoration: none;
color: inherit;
}
.rx-sidebar-post-thumb img {
width: 72px;
height: 72px;
object-fit: cover;
border-radius: 10px;
}
.rx-sidebar-post-title {
display: block;
font-weight: 600;
line-height: 1.4;
}
.rx-sidebar-post-date {
display: block;
margin-top: 4px;
font-size: 13px;
opacity: 0.7;
}
.rx-category-item a {
display: flex;
justify-content: space-between;
gap: 10px;
padding: 8px 0;
text-decoration: none;
color: inherit;
border-bottom: 1px solid rgba(0, 0, 0, 0.06);
}
.rx-tag-cloud {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.rx-tag-link {
display: inline-flex;
padding: 6px 10px;
border-radius: 999px;
background: rgba(0, 0, 0, 0.05);
text-decoration: none;
font-size: 14px;
color: inherit;
}
.rx-author-card {
display: flex;
gap: 14px;
align-items: flex-start;
}
.rx-author-avatar img {
border-radius: 50%;
}
.rx-author-name {
margin: 0 0 8px;
font-size: 18px;
}
.rx-author-description {
margin: 0 0 10px;
}
.rx-newsletter-form {
display: flex;
flex-direction: column;
gap: 10px;
}
.rx-newsletter-form input[type="email"] {
width: 100%;
padding: 12px 14px;
border: 1px solid rgba(0, 0, 0, 0.15);
border-radius: 10px;
}
.rx-newsletter-submit {
padding: 12px 14px;
border: 0;
border-radius: 10px;
cursor: pointer;
font-weight: 700;
}
.rx-ad-box {
min-height: 220px;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
border: 2px dashed rgba(0, 0, 0, 0.15);
border-radius: 12px;
opacity: 0.8;
}
.rx-sidebar-sticky-zone {
position: sticky;
top: 24px;
display: flex;
flex-direction: column;
gap: 24px;
}
@media (max-width: 991px) {
.rx-sidebar {
max-width: 100%;
}
.rx-sidebar-sticky-zone {
position: static;
}
}
Use in your theme templates like this:
<?php get_sidebar(); ?>
This is a very strong sidebar.php base for your RX theme. It is safe, dynamic, SEO-friendly, widget-ready, and ready for ads, medical content, author branding, and article navigation.