template-medical-library.php

I’ll build this as a full WordPress page template for your RX Theme Medical Library: secure queries, filters, search, taxonomy support, featured articles, pagination, schema JSON-LD, accessibility, and fallback handling.

Dear friend, create this file:

rx-theme/templates/template-medical-library.php

Then paste the full code below.

<?php
/**
 * Template Name: RX Medical Library
 * Template Post Type: page
 *
 * Advanced Medical Library template for RX Theme.
 *
 * Features:
 * - Medical article archive layout
 * - Keyword search
 * - Category filter
 * - Topic/tag filter
 * - Specialty filter if custom taxonomy exists
 * - Featured article section
 * - Latest article grid
 * - Pagination
 * - Reading time
 * - Views placeholder support
 * - Breadcrumb support
 * - Schema JSON-LD
 * - Accessible search/filter form
 * - Safe escaping and sanitization
 *
 * @package RX_Theme
 */

defined( 'ABSPATH' ) || exit;

get_header();

/**
 * ------------------------------------------------------------
 * RX Medical Library Helper Functions
 * ------------------------------------------------------------
 */

if ( ! function_exists( 'rx_medical_library_post_type' ) ) {
	/**
	 * Get preferred medical library post type.
	 *
	 * If you later create a custom post type named "medical_library",
	 * this template will automatically use it.
	 *
	 * @return string
	 */
	function rx_medical_library_post_type() {
		return post_type_exists( 'medical_library' ) ? 'medical_library' : 'post';
	}
}

if ( ! function_exists( 'rx_medical_library_excerpt' ) ) {
	/**
	 * Custom safe excerpt.
	 *
	 * @param int $length Excerpt length.
	 * @return string
	 */
	function rx_medical_library_excerpt( $length = 28 ) {
		$excerpt = get_the_excerpt();

		if ( empty( $excerpt ) ) {
			$excerpt = wp_strip_all_tags( get_the_content() );
		}

		return wp_trim_words( $excerpt, absint( $length ), '...' );
	}
}

if ( ! function_exists( 'rx_medical_library_reading_time' ) ) {
	/**
	 * Estimate article reading time.
	 *
	 * @param int $post_id Post ID.
	 * @return string
	 */
	function rx_medical_library_reading_time( $post_id = 0 ) {
		$post_id = $post_id ? absint( $post_id ) : get_the_ID();

		$content    = get_post_field( 'post_content', $post_id );
		$word_count = str_word_count( wp_strip_all_tags( $content ) );
		$minutes    = max( 1, ceil( $word_count / 200 ) );

		return sprintf(
			/* translators: %d: reading time minutes */
			_n( '%d min read', '%d min read', $minutes, 'rx-theme' ),
			$minutes
		);
	}
}

if ( ! function_exists( 'rx_medical_library_get_taxonomies' ) ) {
	/**
	 * Get supported taxonomies for library filters.
	 *
	 * @return array
	 */
	function rx_medical_library_get_taxonomies() {
		$taxonomies = array();

		if ( taxonomy_exists( 'category' ) ) {
			$taxonomies['category'] = __( 'Category', 'rx-theme' );
		}

		if ( taxonomy_exists( 'post_tag' ) ) {
			$taxonomies['post_tag'] = __( 'Topic', 'rx-theme' );
		}

		if ( taxonomy_exists( 'medical_topic' ) ) {
			$taxonomies['medical_topic'] = __( 'Medical Topic', 'rx-theme' );
		}

		if ( taxonomy_exists( 'medical_specialty' ) ) {
			$taxonomies['medical_specialty'] = __( 'Medical Specialty', 'rx-theme' );
		}

		if ( taxonomy_exists( 'disease_group' ) ) {
			$taxonomies['disease_group'] = __( 'Disease Group', 'rx-theme' );
		}

		return $taxonomies;
	}
}

if ( ! function_exists( 'rx_medical_library_render_terms' ) ) {
	/**
	 * Render post term chips.
	 *
	 * @param int   $post_id    Post ID.
	 * @param array $taxonomies Taxonomies.
	 * @return void
	 */
	function rx_medical_library_render_terms( $post_id, $taxonomies = array() ) {
		if ( empty( $taxonomies ) ) {
			$taxonomies = array_keys( rx_medical_library_get_taxonomies() );
		}

		$output = array();

		foreach ( $taxonomies as $taxonomy ) {
			if ( ! taxonomy_exists( $taxonomy ) ) {
				continue;
			}

			$terms = get_the_terms( $post_id, $taxonomy );

			if ( empty( $terms ) || is_wp_error( $terms ) ) {
				continue;
			}

			foreach ( $terms as $term ) {
				$output[] = sprintf(
					'<a class="rx-medical-card__term" href="%1$s">%2$s</a>',
					esc_url( get_term_link( $term ) ),
					esc_html( $term->name )
				);
			}
		}

		if ( ! empty( $output ) ) {
			echo '<div class="rx-medical-card__terms" aria-label="' . esc_attr__( 'Article topics', 'rx-theme' ) . '">';
			echo wp_kses_post( implode( '', array_slice( $output, 0, 4 ) ) );
			echo '</div>';
		}
	}
}

if ( ! function_exists( 'rx_medical_library_thumbnail' ) ) {
	/**
	 * Render card thumbnail.
	 *
	 * @param int    $post_id Post ID.
	 * @param string $size    Image size.
	 * @return void
	 */
	function rx_medical_library_thumbnail( $post_id, $size = 'large' ) {
		if ( has_post_thumbnail( $post_id ) ) {
			echo '<a class="rx-medical-card__image-link" href="' . esc_url( get_permalink( $post_id ) ) . '" aria-label="' . esc_attr( get_the_title( $post_id ) ) . '">';
			echo get_the_post_thumbnail(
				$post_id,
				$size,
				array(
					'class'   => 'rx-medical-card__image',
					'loading' => 'lazy',
					'alt'     => esc_attr( get_the_title( $post_id ) ),
				)
			);
			echo '</a>';
		} else {
			echo '<a class="rx-medical-card__image-link rx-medical-card__image-link--placeholder" href="' . esc_url( get_permalink( $post_id ) ) . '" aria-label="' . esc_attr( get_the_title( $post_id ) ) . '">';
			echo '<span class="rx-medical-card__placeholder-icon" aria-hidden="true">⚕</span>';
			echo '</a>';
		}
	}
}

if ( ! function_exists( 'rx_medical_library_card' ) ) {
	/**
	 * Render article card.
	 *
	 * @param int    $post_id Post ID.
	 * @param string $variant Card variant.
	 * @return void
	 */
	function rx_medical_library_card( $post_id, $variant = 'default' ) {
		?>
		<article id="post-<?php echo esc_attr( $post_id ); ?>" <?php post_class( 'rx-medical-card rx-medical-card--' . sanitize_html_class( $variant ), $post_id ); ?>>

			<?php rx_medical_library_thumbnail( $post_id, 'medium_large' ); ?>

			<div class="rx-medical-card__body">

				<?php rx_medical_library_render_terms( $post_id ); ?>

				<h2 class="rx-medical-card__title">
					<a href="<?php echo esc_url( get_permalink( $post_id ) ); ?>">
						<?php echo esc_html( get_the_title( $post_id ) ); ?>
					</a>
				</h2>

				<div class="rx-medical-card__meta">
					<span class="rx-medical-card__date">
						<?php echo esc_html( get_the_date( '', $post_id ) ); ?>
					</span>

					<span class="rx-medical-card__separator" aria-hidden="true"></span>

					<span class="rx-medical-card__reading-time">
						<?php echo esc_html( rx_medical_library_reading_time( $post_id ) ); ?>
					</span>

					<?php if ( get_the_author_meta( 'display_name', get_post_field( 'post_author', $post_id ) ) ) : ?>
						<span class="rx-medical-card__separator" aria-hidden="true"></span>
						<span class="rx-medical-card__author">
							<?php echo esc_html( get_the_author_meta( 'display_name', get_post_field( 'post_author', $post_id ) ) ); ?>
						</span>
					<?php endif; ?>
				</div>

				<p class="rx-medical-card__excerpt">
					<?php echo esc_html( rx_medical_library_excerpt( 'featured' === $variant ? 42 : 26 ) ); ?>
				</p>

				<a class="rx-medical-card__button" href="<?php echo esc_url( get_permalink( $post_id ) ); ?>">
					<?php esc_html_e( 'Read Article', 'rx-theme' ); ?>
					<span aria-hidden="true"></span>
				</a>

			</div>
		</article>
		<?php
	}
}

/**
 * ------------------------------------------------------------
 * Search and Filter Values
 * ------------------------------------------------------------
 */

$post_type       = rx_medical_library_post_type();
$paged           = max( 1, absint( get_query_var( 'paged' ) ? get_query_var( 'paged' ) : get_query_var( 'page' ) ) );
$search_keyword  = isset( $_GET['rx_search'] ) ? sanitize_text_field( wp_unslash( $_GET['rx_search'] ) ) : '';
$category_filter = isset( $_GET['rx_category'] ) ? absint( $_GET['rx_category'] ) : 0;
$topic_filter    = isset( $_GET['rx_topic'] ) ? absint( $_GET['rx_topic'] ) : 0;
$specialty_filter = isset( $_GET['rx_specialty'] ) ? absint( $_GET['rx_specialty'] ) : 0;

$tax_query = array();

if ( $category_filter && taxonomy_exists( 'category' ) ) {
	$tax_query[] = array(
		'taxonomy' => 'category',
		'field'    => 'term_id',
		'terms'    => $category_filter,
	);
}

if ( $topic_filter && taxonomy_exists( 'post_tag' ) ) {
	$tax_query[] = array(
		'taxonomy' => 'post_tag',
		'field'    => 'term_id',
		'terms'    => $topic_filter,
	);
}

if ( $specialty_filter && taxonomy_exists( 'medical_specialty' ) ) {
	$tax_query[] = array(
		'taxonomy' => 'medical_specialty',
		'field'    => 'term_id',
		'terms'    => $specialty_filter,
	);
}

if ( count( $tax_query ) > 1 ) {
	$tax_query['relation'] = 'AND';
}

/**
 * Main query.
 */
$library_args = array(
	'post_type'           => $post_type,
	'post_status'         => 'publish',
	'posts_per_page'      => 12,
	'paged'               => $paged,
	's'                   => $search_keyword,
	'ignore_sticky_posts' => true,
	'orderby'             => 'date',
	'order'               => 'DESC',
);

if ( ! empty( $tax_query ) ) {
	$library_args['tax_query'] = $tax_query; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
}

$library_query = new WP_Query( $library_args );

/**
 * Featured query.
 */
$featured_args = array(
	'post_type'           => $post_type,
	'post_status'         => 'publish',
	'posts_per_page'      => 1,
	'ignore_sticky_posts' => true,
	'orderby'             => 'date',
	'order'               => 'DESC',
	'meta_query'          => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
		array(
			'key'     => '_rx_featured_article',
			'value'   => '1',
			'compare' => '=',
		),
	),
);

$featured_query = new WP_Query( $featured_args );

if ( ! $featured_query->have_posts() ) {
	$featured_args_fallback = array(
		'post_type'           => $post_type,
		'post_status'         => 'publish',
		'posts_per_page'      => 1,
		'ignore_sticky_posts' => true,
		'orderby'             => 'date',
		'order'               => 'DESC',
	);

	$featured_query = new WP_Query( $featured_args_fallback );
}

?>

<main id="primary" class="site-main rx-medical-library">

	<?php
	if ( function_exists( 'rx_theme_breadcrumbs' ) ) {
		rx_theme_breadcrumbs();
	}
	?>

	<section class="rx-medical-hero" aria-labelledby="rx-medical-library-title">
		<div class="rx-container">

			<p class="rx-medical-hero__eyebrow">
				<?php esc_html_e( 'RX Medical Knowledge Center', 'rx-theme' ); ?>
			</p>

			<h1 id="rx-medical-library-title" class="rx-medical-hero__title">
				<?php
				if ( get_the_title() ) {
					echo esc_html( get_the_title() );
				} else {
					esc_html_e( 'Medical Library', 'rx-theme' );
				}
				?>
			</h1>

			<div class="rx-medical-hero__description">
				<?php
				if ( have_posts() ) {
					while ( have_posts() ) {
						the_post();

						if ( get_the_content() ) {
							the_content();
						} else {
							echo '<p>' . esc_html__( 'Explore simple, evidence-aware medical articles, disease explanations, symptoms, diagnostic tests, treatment guidance, prevention tips, and patient education resources.', 'rx-theme' ) . '</p>';
						}
					}

					wp_reset_postdata();
				}
				?>
			</div>

			<form class="rx-medical-search" method="get" action="<?php echo esc_url( get_permalink() ); ?>" role="search">
				<div class="rx-medical-search__grid">

					<div class="rx-medical-search__field">
						<label for="rx-search-keyword">
							<?php esc_html_e( 'Search medical topic', 'rx-theme' ); ?>
						</label>
						<input
							type="search"
							id="rx-search-keyword"
							name="rx_search"
							value="<?php echo esc_attr( $search_keyword ); ?>"
							placeholder="<?php esc_attr_e( 'Search disease, symptom, test, treatment...', 'rx-theme' ); ?>"
						/>
					</div>

					<?php if ( taxonomy_exists( 'category' ) ) : ?>
						<div class="rx-medical-search__field">
							<label for="rx-search-category">
								<?php esc_html_e( 'Category', 'rx-theme' ); ?>
							</label>
							<?php
							wp_dropdown_categories(
								array(
									'taxonomy'          => 'category',
									'name'              => 'rx_category',
									'id'                => 'rx-search-category',
									'selected'          => $category_filter,
									'show_option_all'   => __( 'All Categories', 'rx-theme' ),
									'hide_empty'        => true,
									'value_field'       => 'term_id',
									'class'             => 'rx-medical-search__select',
								)
							);
							?>
						</div>
					<?php endif; ?>

					<?php if ( taxonomy_exists( 'post_tag' ) ) : ?>
						<div class="rx-medical-search__field">
							<label for="rx-search-topic">
								<?php esc_html_e( 'Topic', 'rx-theme' ); ?>
							</label>
							<?php
							wp_dropdown_categories(
								array(
									'taxonomy'          => 'post_tag',
									'name'              => 'rx_topic',
									'id'                => 'rx-search-topic',
									'selected'          => $topic_filter,
									'show_option_all'   => __( 'All Topics', 'rx-theme' ),
									'hide_empty'        => true,
									'value_field'       => 'term_id',
									'class'             => 'rx-medical-search__select',
								)
							);
							?>
						</div>
					<?php endif; ?>

					<?php if ( taxonomy_exists( 'medical_specialty' ) ) : ?>
						<div class="rx-medical-search__field">
							<label for="rx-search-specialty">
								<?php esc_html_e( 'Specialty', 'rx-theme' ); ?>
							</label>
							<?php
							wp_dropdown_categories(
								array(
									'taxonomy'          => 'medical_specialty',
									'name'              => 'rx_specialty',
									'id'                => 'rx-search-specialty',
									'selected'          => $specialty_filter,
									'show_option_all'   => __( 'All Specialties', 'rx-theme' ),
									'hide_empty'        => true,
									'value_field'       => 'term_id',
									'class'             => 'rx-medical-search__select',
								)
							);
							?>
						</div>
					<?php endif; ?>

					<div class="rx-medical-search__actions">
						<button type="submit" class="rx-medical-search__button">
							<?php esc_html_e( 'Search Library', 'rx-theme' ); ?>
						</button>

						<a class="rx-medical-search__reset" href="<?php echo esc_url( get_permalink() ); ?>">
							<?php esc_html_e( 'Reset', 'rx-theme' ); ?>
						</a>
					</div>

				</div>
			</form>

		</div>
	</section>

	<?php if ( $featured_query->have_posts() ) : ?>
		<section class="rx-medical-featured" aria-labelledby="rx-medical-featured-title">
			<div class="rx-container">

				<div class="rx-section-heading">
					<p class="rx-section-heading__eyebrow">
						<?php esc_html_e( 'Featured Guide', 'rx-theme' ); ?>
					</p>
					<h2 id="rx-medical-featured-title" class="rx-section-heading__title">
						<?php esc_html_e( 'Important Medical Article', 'rx-theme' ); ?>
					</h2>
				</div>

				<?php
				while ( $featured_query->have_posts() ) :
					$featured_query->the_post();
					rx_medical_library_card( get_the_ID(), 'featured' );
				endwhile;

				wp_reset_postdata();
				?>

			</div>
		</section>
	<?php endif; ?>

	<section class="rx-medical-content" aria-labelledby="rx-medical-latest-title">
		<div class="rx-container">

			<div class="rx-medical-content__header">
				<div class="rx-section-heading">
					<p class="rx-section-heading__eyebrow">
						<?php esc_html_e( 'Latest Updates', 'rx-theme' ); ?>
					</p>
					<h2 id="rx-medical-latest-title" class="rx-section-heading__title">
						<?php esc_html_e( 'Medical Library Articles', 'rx-theme' ); ?>
					</h2>
				</div>

				<p class="rx-medical-content__count">
					<?php
					printf(
						/* translators: %d: result count */
						esc_html( _n( '%d article found', '%d articles found', $library_query->found_posts, 'rx-theme' ) ),
						absint( $library_query->found_posts )
					);
					?>
				</p>
			</div>

			<?php if ( $library_query->have_posts() ) : ?>

				<div class="rx-medical-grid">
					<?php
					while ( $library_query->have_posts() ) :
						$library_query->the_post();
						rx_medical_library_card( get_the_ID(), 'default' );
					endwhile;
					?>
				</div>

				<nav class="rx-medical-pagination" aria-label="<?php esc_attr_e( 'Medical library pagination', 'rx-theme' ); ?>">
					<?php
					echo wp_kses_post(
						paginate_links(
							array(
								'total'     => $library_query->max_num_pages,
								'current'   => $paged,
								'mid_size'  => 2,
								'prev_text' => '<span aria-hidden="true">←</span> ' . esc_html__( 'Previous', 'rx-theme' ),
								'next_text' => esc_html__( 'Next', 'rx-theme' ) . ' <span aria-hidden="true">→</span>',
								'add_args'  => array_filter(
									array(
										'rx_search'    => $search_keyword,
										'rx_category'  => $category_filter,
										'rx_topic'     => $topic_filter,
										'rx_specialty' => $specialty_filter,
									)
								),
							)
						)
					);
					?>
				</nav>

				<?php wp_reset_postdata(); ?>

			<?php else : ?>

				<div class="rx-medical-empty">
					<h2 class="rx-medical-empty__title">
						<?php esc_html_e( 'No medical articles found', 'rx-theme' ); ?>
					</h2>
					<p class="rx-medical-empty__text">
						<?php esc_html_e( 'Try another keyword, remove filters, or browse all medical articles.', 'rx-theme' ); ?>
					</p>
					<a class="rx-medical-empty__button" href="<?php echo esc_url( get_permalink() ); ?>">
						<?php esc_html_e( 'View All Articles', 'rx-theme' ); ?>
					</a>
				</div>

			<?php endif; ?>

		</div>
	</section>

	<section class="rx-medical-trust" aria-labelledby="rx-medical-trust-title">
		<div class="rx-container">

			<h2 id="rx-medical-trust-title" class="rx-medical-trust__title">
				<?php esc_html_e( 'How RX Medical Library Helps Readers', 'rx-theme' ); ?>
			</h2>

			<div class="rx-medical-trust__grid">

				<div class="rx-medical-trust__item">
					<h3><?php esc_html_e( 'Simple Medical Language', 'rx-theme' ); ?></h3>
					<p><?php esc_html_e( 'Articles are written in clear and easy language so patients, families, students, and health readers can understand medical topics better.', 'rx-theme' ); ?></p>
				</div>

				<div class="rx-medical-trust__item">
					<h3><?php esc_html_e( 'Doctor-Friendly Structure', 'rx-theme' ); ?></h3>
					<p><?php esc_html_e( 'Content can be arranged by disease, symptom, diagnostic test, treatment, prevention, and medical specialty.', 'rx-theme' ); ?></p>
				</div>

				<div class="rx-medical-trust__item">
					<h3><?php esc_html_e( 'Search Engine Ready', 'rx-theme' ); ?></h3>
					<p><?php esc_html_e( 'The template uses clean headings, article cards, metadata, internal links, and schema-friendly structure for better visibility.', 'rx-theme' ); ?></p>
				</div>

			</div>

		</div>
	</section>

</main>

<?php
/**
 * ------------------------------------------------------------
 * Schema JSON-LD
 * ------------------------------------------------------------
 */

$schema_items = array();

if ( $library_query->have_posts() ) {
	foreach ( $library_query->posts as $index => $schema_post ) {
		$schema_items[] = array(
			'@type'    => 'ListItem',
			'position' => $index + 1,
			'url'      => get_permalink( $schema_post->ID ),
			'name'     => get_the_title( $schema_post->ID ),
		);
	}
}

$schema = array(
	'@context'        => 'https://schema.org',
	'@type'           => 'CollectionPage',
	'name'            => get_the_title() ? get_the_title() : __( 'Medical Library', 'rx-theme' ),
	'description'     => wp_strip_all_tags( get_the_excerpt() ),
	'url'             => get_permalink(),
	'isPartOf'        => array(
		'@type' => 'WebSite',
		'name'  => get_bloginfo( 'name' ),
		'url'   => home_url( '/' ),
	),
	'mainEntity'      => array(
		'@type'           => 'ItemList',
		'numberOfItems'   => count( $schema_items ),
		'itemListElement' => $schema_items,
	),
);

?>
<script type="application/ld+json">
<?php echo wp_json_encode( $schema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT ); ?>
</script>

<?php
get_footer();

Important note, dear friend

This template is ready now, but for best advanced RX Theme system, later you should also create:

inc/post-types/medical-library.php
inc/taxonomies/medical-taxonomies.php
assets/css/templates/medical-library.css
assets/js/templates/medical-library.js

Because the template already supports these future custom taxonomies:

medical_topic
medical_specialty
disease_group

And it supports this future featured article custom field:

_rx_featured_article = 1

So today it will work with normal WordPress posts, categories, and tags. Later, when you create the custom post type medical_library, it will automatically use that advanced medical post type.

Leave a Reply

Your email address will not be published. Required fields are marked *