File: /var/www/e-tdr.com/e-tdr/wp-content/themes/e-tdr/blog.php
<?php
$blog_page_id = get_the_ID();
$blog_page_title = get_the_title($blog_page_id);
$blog_page_desc = get_the_excerpt($blog_page_id);
if (empty($blog_page_title)) {
$blog_page_title = 'Insights & Updates';
}
if (empty($blog_page_desc)) {
$blog_page_desc = 'Expert perspectives on eTDR, blockchain land records, smart cities, and digital governance in India.';
}
$blog_page_url = get_permalink($blog_page_id);
$paged = 1;
if (get_query_var('paged')) {
$paged = get_query_var('paged');
} elseif (get_query_var('page')) {
$paged = get_query_var('page');
}
$paged = max(1, (int) $paged);
$active_cat_id = isset($_GET['cat']) ? (int) $_GET['cat'] : 0;
$active_cat_obj = $active_cat_id ? get_category($active_cat_id) : null;
$active_cat_name = ($active_cat_obj && ! is_wp_error($active_cat_obj)) ? $active_cat_obj->name : '';
$search_query = isset($_GET['blog_search']) ? sanitize_text_field($_GET['blog_search']) : '';
$posts_per_page = 10;
$query_args = [
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => $posts_per_page,
'paged' => $paged,
'ignore_sticky_posts' => 0,
];
if ($active_cat_id) {
$query_args['cat'] = $active_cat_id;
}
if ($search_query) {
$query_args['s'] = $search_query;
}
$blog_query = new WP_Query($query_args);
$total_posts = (int) $blog_query->found_posts;
$total_pages = (int) $blog_query->max_num_pages;
$hero_title = $blog_page_title;
$hero_desc = $blog_page_desc;
$hero_label = 'eTDR Blog';
if ($active_cat_name) {
$hero_title = esc_html($active_cat_name);
$hero_desc = '';
$hero_label = 'Category';
}
if ($search_query) {
$hero_title = 'Results for “' . esc_html($search_query) . '”';
$hero_desc = '';
$hero_label = 'Search Results';
}
$all_url = $blog_page_url;
$grad_classes = ['apc-img-1', 'apc-img-2', 'apc-img-3', 'apc-img-4', 'apc-img-5', 'apc-img-6'];
$grad_icons = ['bi-buildings-fill', 'bi-cpu', 'bi-shield-fill-check', 'bi-graph-up-arrow', 'bi-map', 'bi-currency-exchange'];
function ec_blog_page_url($page_num, $base_url, $extra_args = [])
{
if ($page_num <= 1) {
return empty($extra_args) ? $base_url : add_query_arg($extra_args, $base_url);
}
$args = array_merge($extra_args, ['page' => $page_num]);
return add_query_arg($args, $base_url);
}
get_header();
?>
<section class="arch-hero">
<div class="arch-hero-bg"></div>
<div class="container arch-hero-inner">
<!-- Breadcrumb -->
<nav class="arch-breadcrumb" aria-label="breadcrumb">
<a href="<?php echo esc_url(home_url('/')); ?>">
<i class="bi bi-house-fill"></i> Home
</a>
<span class="arch-bc-sep"><i class="bi bi-chevron-right"></i></span>
<?php if ($active_cat_name || $search_query) : ?>
<a href="<?php echo esc_url($blog_page_url); ?>">
<?php echo esc_html(get_the_title($blog_page_id) ?: 'Insights'); ?>
</a>
<span class="arch-bc-sep"><i class="bi bi-chevron-right"></i></span>
<span><?php echo $active_cat_name ? esc_html($active_cat_name) : 'Search'; ?></span>
<?php else : ?>
<span><?php echo esc_html(get_the_title($blog_page_id) ?: 'Insights'); ?></span>
<?php endif; ?>
</nav>
<!-- Title row -->
<div class="arch-hero-title-row">
<div>
<div class="arch-hero-label">
<i class="bi bi-<?php
echo $active_cat_name ? 'tag' : ($search_query ? 'search' : 'newspaper');
?>"></i>
<?php echo esc_html($hero_label); ?>
</div>
<h1 class="arch-hero-h1"><?php echo $hero_title; ?></h1>
<?php if ($hero_desc) : ?>
<p class="arch-hero-desc"><?php echo wp_kses_post($hero_desc); ?></p>
<?php endif; ?>
</div>
</div>
<!-- Search bar -->
<form class="arch-search-bar" role="search" method="get"
action="<?php echo esc_url($blog_page_url); ?>">
<?php if ($active_cat_id) : ?>
<input type="hidden" name="cat" value="<?php echo $active_cat_id; ?>">
<?php endif; ?>
<i class="bi bi-search arch-search-icon"></i>
<input
type="search"
name="blog_search"
class="arch-search-input"
placeholder="Search articles, topics, keywords…"
value="<?php echo esc_attr($search_query); ?>"
autocomplete="off" />
<button type="submit" class="arch-search-btn">Search</button>
</form>
</div>
</section><!-- /arch-hero -->
<?php
$filter_cats = get_categories([
'hide_empty' => true,
'orderby' => 'count',
'order' => 'DESC',
'number' => 10,
]);
?>
<?php if ($filter_cats) : ?>
<div class="arch-filter-bar">
<div class="container">
<div class="arch-filter-wrap">
<span class="arch-filter-label"><i class="bi bi-funnel"></i> Filter:</span>
<div class="arch-filter-pills">
<!-- All -->
<a href="<?php echo esc_url($all_url); ?>"
class="arch-filter-pill <?php echo ! $active_cat_id ? 'active' : ''; ?>">
All
</a>
<?php foreach ($filter_cats as $fc) : ?>
<a href="<?php echo esc_url(add_query_arg('cat', $fc->term_id, $blog_page_url)); ?>"
class="arch-filter-pill <?php echo $active_cat_id === (int) $fc->term_id ? 'active' : ''; ?>">
<?php echo esc_html($fc->name); ?>
<span class="arch-pill-count"><?php echo (int) $fc->count; ?></span>
</a>
<?php endforeach; ?>
</div>
<!-- Mobile Category Dropdown -->
<div class="arch-filter-mobile">
<select class="arch-cat-select" onchange="location = this.value;">
<option value="<?php echo esc_url($all_url); ?>" <?php selected($active_cat_id, 0); ?>>All Categories</option>
<?php foreach ($filter_cats as $fc) : ?>
<option value="<?php echo esc_url(add_query_arg('cat', $fc->term_id, $blog_page_url)); ?>" <?php selected($active_cat_id, (int)$fc->term_id); ?>>
<?php echo esc_html($fc->name); ?> (<?php echo (int) $fc->count; ?>)
</option>
<?php endforeach; ?>
</select>
</div>
</div>
</div>
</div>
<?php endif; ?>
<div class="arch-body">
<div class="container">
<div class="arch-layout no-sidebar">
<main class="arch-main" id="main">
<?php if ($blog_query->have_posts()) : ?>
<?php
/* Show featured card only on page 1, no filter, no search */
$show_featured = ($paged === 1 && ! $active_cat_id && ! $search_query);
$post_index = 0;
?>
<?php while ($blog_query->have_posts()) : $blog_query->the_post(); ?>
<?php
$p_cats = get_the_category();
$p_cat_name = $p_cats ? $p_cats[0]->name : 'Article';
$p_cat_link = $p_cats ? add_query_arg('cat', $p_cats[0]->term_id, $blog_page_url) : '#';
$p_words = str_word_count(strip_tags(get_the_content()));
$p_readtime = max(1, ceil($p_words / 200));
$p_thumb = has_post_thumbnail();
$p_grad = $grad_classes[$post_index % count($grad_classes)];
$p_icon = $grad_icons[$post_index % count($grad_icons)];
?>
<?php if ($show_featured && $post_index === 0) : ?>
<article class="apc-featured" id="post-<?php the_ID(); ?>">
<div class="apc-featured-img-wrap">
<?php if ($p_thumb) : ?>
<?php the_post_thumbnail('full', ['class' => 'apc-featured-img-real']); ?>
<?php else : ?>
<div class="apc-featured-img-placeholder <?php echo $p_grad; ?>">
<i class="bi <?php echo $p_icon; ?>"></i>
</div>
<?php endif; ?>
</div>
<div class="apc-featured-body">
<div class="apc-featured-meta">
<span class="apc-featured-date">
<i class="bi bi-calendar3"></i> <?php echo get_the_date('d M Y'); ?>
</span>
<span class="apc-featured-read">
<i class="bi bi-clock"></i> <?php echo $p_readtime; ?> min read
</span>
</div>
<h2 class="apc-featured-title">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</h2>
<p class="apc-featured-excerpt">
<?php echo wp_trim_words(get_the_excerpt() ?: strip_tags(get_the_content()), 32, '…'); ?>
</p>
<a href="<?php the_permalink(); ?>" class="apc-featured-link">
Read More <i class="bi bi-arrow-right"></i>
</a>
</div>
</article>
<!-- Grid heading -->
<?php if ($total_posts > 1) : ?>
<div class="arch-grid-head">
<h2 class="arch-grid-title">More Articles</h2>
</div>
<?php endif; ?>
<?php else : /* ── STANDARD GRID CARD ── */ ?>
<?php if ($post_index === ($show_featured ? 1 : 0)) : ?>
<div class="arch-grid">
<?php endif; ?>
<article class="apc-card" id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink(); ?>" class="apc-card-img-wrap" tabindex="-1" aria-hidden="true" style="display:block; overflow:hidden;">
<?php if ($p_thumb) : ?>
<?php the_post_thumbnail('full', ['class' => 'apc-card-img-real', 'style' => 'height:auto !important; aspect-ratio: 16/9; object-fit: cover;']); ?>
<?php else : ?>
<div class="apc-card-img-placeholder <?php echo $p_grad; ?>" style="height:auto !important; aspect-ratio: 16/9;">
<i class="bi <?php echo $p_icon; ?>"></i>
</div>
<?php endif; ?>
</a>
<div class="apc-card-body">
<div class="apc-card-meta-top">
<a href="<?php echo esc_url($p_cat_link); ?>" class="apc-card-cat">
<?php echo esc_html($p_cat_name); ?>
</a>
<span class="apc-card-date">
<i class="bi bi-calendar3"></i> <?php echo get_the_date('d M Y'); ?>
</span>
</div>
<h3 class="apc-card-title">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</h3>
<p class="apc-card-excerpt">
<?php echo wp_trim_words(get_the_excerpt() ?: strip_tags(get_the_content()), 18, '…'); ?>
</p>
<div class="apc-card-footer">
<span class="apc-card-read">
<i class="bi bi-clock"></i> <?php echo $p_readtime; ?> min
</span>
<a href="<?php the_permalink(); ?>" class="apc-card-link">
Read More <i class="bi bi-arrow-right"></i>
</a>
</div>
</div>
</article>
<?php endif; /* end featured vs grid */ ?>
<?php $post_index++;
endwhile;
wp_reset_postdata(); ?>
<?php if ($post_index > ($show_featured ? 1 : 0)) : ?>
</div>
<?php endif; ?>
<?php if ($total_pages > 1) : ?>
<nav class="arch-pagination" aria-label="Posts navigation">
<ul class="page-numbers">
<?php if ($paged > 1) : ?>
<li>
<a class="prev page-numbers"
href="<?php echo esc_url(ec_blog_page_url(
$paged - 1,
$blog_page_url,
array_filter(['cat' => $active_cat_id, 'blog_search' => $search_query])
)); ?>">
<i class="bi bi-chevron-left"></i> Previous
</a>
</li>
<?php endif; ?>
<?php
/* Build page number links: first, last, and a window around current */
$window = 2;
$pages_to_show = [];
$pages_to_show[] = 1;
$pages_to_show[] = $total_pages;
for ($i = max(2, $paged - $window); $i <= min($total_pages - 1, $paged + $window); $i++) {
$pages_to_show[] = $i;
}
$pages_to_show = array_unique($pages_to_show);
sort($pages_to_show);
$prev_p = 0;
foreach ($pages_to_show as $p) :
if ($p - $prev_p > 1) : ?>
<li><span class="page-numbers dots">…</span></li>
<?php endif;
$extra = array_filter(['cat' => $active_cat_id, 'blog_search' => $search_query]);
if ($p === $paged) : ?>
<li><span class="page-numbers current"><?php echo $p; ?></span></li>
<?php else : ?>
<li>
<a class="page-numbers"
href="<?php echo esc_url(ec_blog_page_url($p, $blog_page_url, $extra)); ?>">
<?php echo $p; ?>
</a>
</li>
<?php endif;
$prev_p = $p;
endforeach;
?>
<?php if ($paged < $total_pages) : ?>
<li>
<a class="next page-numbers"
href="<?php echo esc_url(ec_blog_page_url(
$paged + 1,
$blog_page_url,
array_filter(['cat' => $active_cat_id, 'blog_search' => $search_query])
)); ?>">
Next <i class="bi bi-chevron-right"></i>
</a>
</li>
<?php endif; ?>
</ul>
</nav>
<?php endif; ?>
<?php else : /* ── EMPTY STATE ── */ ?>
<div class="arch-empty">
<div class="arch-empty-icon">
<i class="bi bi-<?php echo $search_query ? 'search' : 'inbox'; ?>"></i>
</div>
<h3 class="arch-empty-title">
<?php echo $search_query ? 'No results found' : 'No articles yet'; ?>
</h3>
<p class="arch-empty-desc">
<?php if ($search_query) : ?>
Nothing matched “<strong><?php echo esc_html($search_query); ?></strong>”.
Try different keywords or browse all articles.
<?php elseif ($active_cat_name) : ?>
No articles in <strong><?php echo esc_html($active_cat_name); ?></strong> yet. Check back soon.
<?php else : ?>
No articles published yet. Check back soon.
<?php endif; ?>
</p>
<div class="arch-empty-actions">
<a href="<?php echo esc_url($blog_page_url); ?>" class="btn-cta-orange">
<i class="bi bi-grid"></i> Browse All Articles
</a>
</div>
</div>
<?php endif; ?>
</main>
</div>
</div>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>